Author |
Topic |
KnooKie
Aged Yak Warrior
623 Posts |
Posted - 2012-06-29 : 07:52:52
|
Hi AllI am trying achieve this sort of logic in a query...SELECT FirstName, LastNameFROM NamesINNER JOIN cmp ON Names.ID = cmp.IDWHERE cmpID IN (1,2,3,4)AND cmpID NOT IN (5,6,7,8) I am wondering if i need to use some sort of NOT EXISTS instead of the NOT IN part of the where clauseAny pointers greatly appreciated====Paul |
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2012-06-29 : 07:56:37
|
No - but yoou also don't need the andWHERE cmpID IN (1,2,3,4)is the same asWHERE cmpID IN (1,2,3,4)and cmpID not in (5,6,7,8)is the same asWHERE cmpID between 1 and 4 -- assuming it is an integer==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
KnooKie
Aged Yak Warrior
623 Posts |
Posted - 2012-06-29 : 08:16:52
|
Sorry my example wasn't very goodThe query is actually more complex than that.We have people who cover multiple cmpIDs (campaign IDs) and i want to exclude certain cmpIDs.For exampleJoe Bloggs may cover cmpIDs 1,2,3,4,6,7and John Doe may cover cmpIDs 1,2,3,4i would like to see only people who have covered cmpIDs 1,2,3,4 and exclude any one who covers cmpIDs 5,6,7,8So Joe Bloggs would not show in the results even though he covers cmpIDs 1-4====Paul |
 |
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-06-29 : 08:58:37
|
May be you do need to use EXISTS and/or NOT EXISTS constructs. For example, may be this?SELECT FirstName, LastNameFROM Names nWHERE EXISTS (SELECT * FROM cmp c WHERE c.cmpId = n.cmpId AND c.cmpId IN (1,2,3,4)) AND NOT EXISTS ( SELECT * FROM cmp c WHERE c.cmpId = n.cmpId AND c.cmpId IN (5,6,7,8)); |
 |
|
KnooKie
Aged Yak Warrior
623 Posts |
Posted - 2012-06-29 : 09:09:59
|
Thanks - i'll give that a goquote: Originally posted by sunitabeck May be you do need to use EXISTS and/or NOT EXISTS constructs. For example, may be this?SELECT FirstName, LastNameFROM Names nWHERE EXISTS (SELECT * FROM cmp c WHERE c.cmpId = n.cmpId AND c.cmpId IN (1,2,3,4)) AND NOT EXISTS ( SELECT * FROM cmp c WHERE c.cmpId = n.cmpId AND c.cmpId IN (5,6,7,8));
====Paul |
 |
|
|
|
|