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
 General SQL Server Forums
 New to SQL Server Programming
 simple query

Author  Topic 

sqlhelp14
Yak Posting Veteran

55 Posts

Posted - 2009-11-30 : 14:51:31
i know its simple for u but i don't get results...

i have one sql statement -

select codeid, codetitle, codedesc from codebase where codeid = '10A' --getting 7 rows

select codeid, salesno, salesdesc, quantity from salesbase where codeid = '10A' -- getting 747 rows

select * from codebase c
left outer join salesbase s
on c.codeid = s.codeid
where c.codeid= '10A'
--its giving me 5229 rows..instead of that i want just 7rows as correct results so what will be my query? any suggestions? please..thanks

sql-programmers
Posting Yak Master

190 Posts

Posted - 2009-11-30 : 15:03:05
The first thing that I would try is using an inner join instead of an left outer join. In addition, you may need to use the distinct keyword when selecting the fields from the codebase table.

SQL Server Programmers and Consultants
http://www.sql-programmers.com/
Go to Top of Page

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2009-11-30 : 15:13:41

IF there can be more than one salesno, salesdesc, etc associated with each codeid, you'll return more than 7 records, even with inner join or distinct. What is it you're really after?

SELECT cb.codeid, cb.codetitle, cb.codedesc ,s.qty

FROM codebase cb

INNER JOIN
(select codeid, [qty] = sum(quantity) from salesbase where codeid = '10A' group by codeid) s
ON
cb.codeid = s.codeid

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page
   

- Advertisement -