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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Where clause on multiple rows of same column

Author  Topic 

kneekill
Yak Posting Veteran

76 Posts

Posted - 2009-09-23 : 10:45:57
Hi

In the below table i need to select the IDs which have both 'q' and 'w' as code


create table #test(id int , code varchar(100))

insert into #test (id,code)
select 1,'q' union all
select 1,'w' union all
select 2,'q' union all
select 2,'r' union all
select 3,'w' union all
select 3,'t' union all
select 4,'q' union all
select 4,'w' union all
select 4,'r' union all
select 5,'t'

select * from #test


ID CODE

1 q
1 w
2 q
2 r
3 w
3 t
4 q
4 w
4 r
5 t

My output of the select should be.PLs help me write the query

ID
1
4

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2009-09-23 : 10:56:12
select t1.id
from
#test t
inner join
( select id,[num] =count(*) from #test where code in ('q','w') group by id having count(*) > 1)t1
on
t.id = t1.id
group by t1.id

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

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')t1
join
(select id,code from #test where code='w')t2
on 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.
Go to Top of Page

kneekill
Yak Posting Veteran

76 Posts

Posted - 2009-09-23 : 11:07:31
Thanks webfred

So if need to do the same check on 3 different codes then we need make
having count(*) = 3 ?



quote:
Originally posted by jimf

select t1.id
from
#test t
inner join
( select id,[num] =count(*) from #test where code in ('q','w') group by id having count(*) > 1)t1
on
t.id = t1.id
group by t1.id

Jim

Everyday I learn something that somebody else already knew

Go to Top of Page

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2009-09-23 : 11:13:57
Yep, that's it

select t1.id
from
#test t
inner join
( select id,[num] =count(*) from #test where code in ('q','w','r') group by id having count(*) > 2)t1
on
t.id = t1.id
group by t1.id

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

kneekill
Yak Posting Veteran

76 Posts

Posted - 2009-09-23 : 11:38:39
Thanks a lot Jim.. just need one more help

if 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 be

ID
--
2
3
5


quote:
Originally posted by jimf

Yep, that's it

select t1.id
from
#test t
inner join
( select id,[num] =count(*) from #test where code in ('q','w','r') group by id having count(*) > 2)t1
on
t.id = t1.id
group by t1.id

Jim

Everyday I learn something that somebody else already knew

Go to Top of Page

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 latter

select t.id
from
#test t
left join
( select id,[num] =count(*) from #test where code in ('q','w') group by id having count(*) > 1)t1
on
t.id = t1.id
where t1.num is null
group by t.id


select t.id
from
#test t
where t.id not in
( select id from #test where code in ('q','w') group by id having count(*) > 1)


Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

kneekill
Yak Posting Veteran

76 Posts

Posted - 2009-09-23 : 12:45:13
Thanks a lot Jim for your help

I have added 'distinct' to your second query to get unique t.id
Go to Top of Page
   

- Advertisement -