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 |
|
workindan
Starting Member
21 Posts |
Posted - 2012-02-22 : 13:53:54
|
| Looking to use Having() at the end of a select query to include a set of values and exclude a set of values...right now I have:(Sybase)************SELECT p.IDFROM people as p inner join items i on p.item_id = i.item_idWHERE p.order_date between '2012-02-20' and '2012-02-22'GROUP BY p.IDhaving count(case when i.item_id in ( set1 of item id's ) then 1 else 0 end) > 0and count(case when i.item_id in ( set2 of item id's ) then 1 else 0 end) > 0and count(case when i.item_id in ( set3 of item id's ) then 1 else 0 end) = 0************So basically I'm looking for people who ordered items with something from set 1 and something from set 2 but did NOT order anything from set 3.When I run this, I get zero results. If I run only the inclusion sets (1 and 2) I get results. If I change set3 count to > 0 to include it, I get results that include set 3.Why can't I use a count of zero to exclude people who ordered something from set 3? |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
workindan
Starting Member
21 Posts |
Posted - 2012-02-22 : 14:31:02
|
| Sorry, it's just a set of ID #'sSELECT p.IDFROM people as p inner join items i on p.item_id = i.item_idWHERE p.order_date between '2012-02-20' and '2012-02-22'GROUP BY p.IDhaving count(case when i.item_id in ('20034','20035','20036',... ) then 1 else 0 end) > 0and count(case when i.item_id in ( '20042','20043','20044',.... ) then 1 else 0 end) > 0and count(case when i.item_id in ( '67889','67890',67891',... ) then 1 else 0 end) = 0 |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
|
|
|
|
|