Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 join problem!

Author  Topic 

bukworm
Starting Member

10 Posts

Posted - 2011-06-13 : 10:00:12
Hi,

All i want is similar orderkeys from tables PDETAIL & ODETAIL, THE SHID is the table ORDER-which has onli one line per order.

THE RESULT should return 20-26, coz tats the onli data in both the pdetail & odetail.

but actually it returns 520. can any1 find the problem. i tried diffrent joins all with same result.

select count(O.KEY), COUNT(p.KEY) from

ODETAIL od
join PDETAIL p on p.KEY = od.KEY

join ORDER O on O.KEY = od.OKEY
where O.SHID = '1057'


thanks

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2011-06-13 : 10:29:48
try
select count(distinct O.KEY), COUNT(distinct p.KEY)



==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

bukworm
Starting Member

10 Posts

Posted - 2011-06-13 : 12:56:57
thank you, tat worked like a charm...
Go to Top of Page

bukworm
Starting Member

10 Posts

Posted - 2011-06-14 : 00:13:57
will this apply for SUM also? if i put sum its sumhow multiplying. I fi put distinct it wont take all the values present only the unique :(.Why does tis happen.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2011-06-14 : 00:26:03
ODETAIL and PDETAIL will produce a kind of CROSS JOIN since they share same key value.



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-06-14 : 02:33:39
You probably miss one more join condition between ODETAIL and PDETAIL

select count(O.KEY), COUNT(p.KEY)
from ODETAIL od
join PDETAIL p on p.KEY = od.KEY AND p.somecol = od.somecol
join ORDER O on O.KEY = od.OKEY
where O.SHID = '1057'



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -