| Author |
Topic |
|
kneekill
Yak Posting Veteran
76 Posts |
Posted - 2009-09-23 : 10:45:57
|
HiIn the below table i need to select the IDs which have both 'q' and 'w' as codecreate table #test(id int , code varchar(100))insert into #test (id,code)select 1,'q' union all select 1,'w' union allselect 2,'q' union all select 2,'r' union allselect 3,'w' union allselect 3,'t' union all select 4,'q' union allselect 4,'w' union allselect 4,'r' union all select 5,'t' select * from #test ID CODE1 q1 w2 q2 r3 w3 t4 q4 w4 r5 tMy output of the select should be.PLs help me write the queryID14 |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2009-09-23 : 10:56:12
|
| select t1.id from #test tinner join ( select id,[num] =count(*) from #test where code in ('q','w') group by id having count(*) > 1)t1on t.id = t1.idgroup by t1.idJimEveryday I learn something that somebody else already knew |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2009-09-23 : 10:57:37
|
select t1.id from(select id,code from #test where code='q')t1join(select id,code from #test where code='w')t2on t2.id=t1.id and t2.code='w'edit: correction No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
kneekill
Yak Posting Veteran
76 Posts |
Posted - 2009-09-23 : 11:07:31
|
Thanks webfredSo if need to do the same check on 3 different codes then we need makehaving count(*) = 3 ?quote: Originally posted by jimf select t1.id from #test tinner join ( select id,[num] =count(*) from #test where code in ('q','w') group by id having count(*) > 1)t1on t.id = t1.idgroup by t1.idJimEveryday I learn something that somebody else already knew
|
 |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2009-09-23 : 11:13:57
|
| Yep, that's itselect t1.id from #test tinner join( select id,[num] =count(*) from #test where code in ('q','w','r') group by id having count(*) > 2)t1ont.id = t1.idgroup by t1.idJimEveryday I learn something that somebody else already knew |
 |
|
|
kneekill
Yak Posting Veteran
76 Posts |
Posted - 2009-09-23 : 11:38:39
|
Thanks a lot Jim.. just need one more helpif i need to query the IDs which do not have the combination codes 'q' and 'w' ..what should i do, count will not work..!i.e my output should beID--235quote: Originally posted by jimf Yep, that's itselect t1.id from #test tinner join( select id,[num] =count(*) from #test where code in ('q','w','r') group by id having count(*) > 2)t1ont.id = t1.idgroup by t1.idJimEveryday I learn something that somebody else already knew
|
 |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2009-09-23 : 11:53:34
|
There's a couple of ways to do it, I prefer the latterselect t.id from #test tleft join( select id,[num] =count(*) from #test where code in ('q','w') group by id having count(*) > 1)t1ont.id = t1.id where t1.num is nullgroup by t.id select t.id from #test twhere t.id not in ( select id from #test where code in ('q','w') group by id having count(*) > 1) JimEveryday I learn something that somebody else already knew |
 |
|
|
kneekill
Yak Posting Veteran
76 Posts |
Posted - 2009-09-23 : 12:45:13
|
| Thanks a lot Jim for your helpI have added 'distinct' to your second query to get unique t.id |
 |
|
|
|