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 |
|
Buzz
Starting Member
2 Posts |
Posted - 2010-06-21 : 14:35:31
|
| Hi, I have been spining my wheels trying to figure out what should be a simple soloution. So instead of sitting here trying the same things over and over I figured I should just ask someone for help lol. Anyway here is the issue I am having...I have a table with columns RepID, CustomerID, SaleType, Office, etcnow each Rep can have multilpe Customers which could have multilpe salestypes. I want to return the customerID if saletype is "Books" and "Pencils"RepID --- CustomerID --- SaleType --- Office1 ------- abc ------- Books ------- CANADA1 ------- abc ------- Pencils ------- USA1 ------- efg ------- Books ------- CANADA2 ------- hij ------- Books ------- USA2 ------- hij ------- bags ------- USAThank you for you time and much needed help |
|
|
Kristen
Test
22859 Posts |
Posted - 2010-06-21 : 14:53:21
|
| [code]SELECT DISTINCT T1.CustomerIDFROM MyTable AS T1 JOIN MyTable AS T2 ON T2.CustomerID = T1.CustomerID AND T2.SaleType = 'Pencils'WHERE T1.SaleType = 'Books'[/code]would work for two categories, not very scalable if you want "n" categories.It would be better if you moved SaleType out of that table into a Child Table instead ("normalisation") |
 |
|
|
Amber_Deslaurier
Starting Member
40 Posts |
Posted - 2010-06-21 : 19:46:59
|
| I think this is what you are looking for...SelectCustomerID,SaleTypeFROM YOURTABLEWHERE SaleType IN (Books, Pencils) ' If you want either one |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2010-06-22 : 03:53:18
|
quote: Originally posted by Amber_DeslaurierIf you want either one
"if saletype is "Books" and "Pencils"" suggests both to me, rather than either - but the O/P can clarify |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-06-22 : 04:46:39
|
| select customerid from tablewhere saletype in ('Books' , 'Pencils')group by customerid having count(distinct saletype )=2MadhivananFailing to plan is Planning to fail |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2010-06-22 : 06:54:25
|
| I knew there was a slicker way! |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-06-22 : 08:24:38
|
quote: Originally posted by Kristen I knew there was a slicker way!
MadhivananFailing to plan is Planning to fail |
 |
|
|
Buzz
Starting Member
2 Posts |
Posted - 2010-06-22 : 11:14:28
|
quote: Originally posted by madhivanan select customerid from tablewhere saletype in ('Books' , 'Pencils')group by customerid having count(distinct saletype )=2MadhivananFailing to plan is Planning to fail
Thank you, that works prefectly! |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-06-23 : 02:51:26
|
quote: Originally posted by Buzz
quote: Originally posted by madhivanan select customerid from tablewhere saletype in ('Books' , 'Pencils')group by customerid having count(distinct saletype )=2MadhivananFailing to plan is Planning to fail
Thank you, that works prefectly!
You are welcome MadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|
|
|