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
 Subqueries

Author  Topic 

doobybug
Starting Member

4 Posts

Posted - 2009-02-23 : 02:37:21
Hi all,

I am a very new student to this stuff, and I need some help while I was experimenting. I have 2 tables called:
tblDistributor
- DNO (PK)
- name
- telno

and

tblDistributor_Model
-DNO(FK)
-MNO(FK)

I have to find all the distributors who currently distribute our currently distributed products by name. I have written something like this which returns the distributor id and the amount of models that he distributes. How can I get the name as well?

SELECT count(*) AS Total_Models
FROM tblDistributor_Model dm, tblDistributor d
GROUP BY dm.DNO
HAVING COUNT (*) = (SELECT DISTINCT COUNT(*)
FROM tblDistributor)

karthik_padbanaban
Constraint Violating Yak Guru

263 Posts

Posted - 2009-02-23 : 02:52:23
--select sum(p),a from @UNIT group by a
declare @tblDistributor table (DNO int,name varchar(100))
insert into @tblDistributor values(1,'a')
insert into @tblDistributor values(2,'b')

SOME THING LIKE THIS.

declare @tblDistributor_Model table (DNO int,MNO int)
insert into @tblDistributor_Model VALUES (1,1)
insert into @tblDistributor_Model VALUES (1,2)
insert into @tblDistributor_Model VALUES (1,3)
insert into @tblDistributor_Model VALUES (2,1)
insert into @tblDistributor_Model VALUES (2,2)

SELECT count(*) AS Total_Models,D.NAME FROM @tblDistributor_Model dm JOIN @tblDistributor d
ON DM.DNO = D.DNO
GROUP BY dm.DNO,D.NAME


Karthik
Go to Top of Page

doobybug
Starting Member

4 Posts

Posted - 2009-02-23 : 03:18:38
Thanks a lot!!! Worked perfectly! :)
Go to Top of Page
   

- Advertisement -