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.
Author |
Topic |
lisakrebs
Starting Member
10 Posts |
Posted - 2010-10-19 : 13:26:47
|
I have 3 tables (Customer, Cust_Relation, and Sales_Rep). I need to to find all the customers that have more than 1 sales reps assigned. I need to use the Cust_Relation table because its the only way to link Customer and Sales_Rep.Here is a very small sampling of data for each table along with my expected results. Customer:Customer ID Customer Name1 John Doe2 John Smith3 Jane DoeCust_Relation:Customer ID Sales Rep ID1 11 22 13 23 4Sales_Rep:Sales Rep ID Rep Name1 House Account2 Bryan Brown3 Aaron Holtz4 Eric Zavilla5 Chip HurtResult Set:Customer ID Customer Name Rep Name1 John Doe House Account1 John Doe Bryan Brown3 Jane Doe Bryan Brown3 Jane Doe Eric ZavillaQuery:select c.customerid, c.customername, s.repnamefrom customer cwhere 1 > (select count(*) from salesrep sr inner join cust_relation cr on cr.repid = sr.repid inner join customer c on c.customerid = cr.customerid)Thanks for any and all feedback!!! |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-10-19 : 13:35:31
|
[code]SELECT c.customerid, c.customernameFROM Customer cINNER JOIN (SELECT Customer_ID,COUNT(Sales_Rep_ID) AS RepCnt FROM Cust_Relation GROUP BY Customer_ID)c1On c1.Customer_ID = c.Customer_IDWHERE c1.repCnt >1[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
lisakrebs
Starting Member
10 Posts |
Posted - 2010-10-19 : 15:04:06
|
That worked perfectly, thanks.  Have one more question since I need to pass this to the user community to review I have to add the sales rep name. When I do that I loose records (approximately 300) from the original query what am I doing wrong (highlighted in red is what I added):select c.customerid, c.name1, c2.salesrepfrom customer cinner join (select customerid, salesrepid, count(caesaruserid) as repcnt from cust_relation group by customerid, salesrepid) c1on c1.customerid = c.customeridleft outer join sales_rep c2 on c1.salesrepid = c2.salesrepidwhere c1.repcnt > 1Thanks! |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-10-19 : 15:08:16
|
in that case better to do like thisSELECT customerid,name1,salesrepFROM(SELECT c.customerid, c.name1, sr.salesrep,COUNT(sr.salesrep) OVER (PARTITION BY c.customerid) AS SRepCntFROM Customer cINNER JOIN cust_relation crON cr.customerid = c.customeridINNER JOIN sales_rep sron sr.salesrepid = cr.salesrepid)tWHERE SRepCnt > 1 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
lisakrebs
Starting Member
10 Posts |
Posted - 2010-10-19 : 15:28:57
|
Perfect, thanks for all your help  |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-10-20 : 12:52:42
|
welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|