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 |
|
vijayakumar_svk
Yak Posting Veteran
50 Posts |
Posted - 2008-12-17 : 23:50:53
|
| I have a table of dataID GroupID-----------1 11 21 32 12 24 14 14 15 15 15 25 3From this above data I want to get the IDs which exist in all groups (1,2,3).Expected resultID15Thanks in advanceVijay==============================Work smarter not harder take control of your life be a super achiever |
|
|
vijayakumar_svk
Yak Posting Veteran
50 Posts |
Posted - 2008-12-17 : 23:54:55
|
| I have this solution..select id from x where id1=1intersect select id from x where id1=2intersect select id from x where id1=3I am looking if any alternate solution.==============================Work smarter not harder take control of your life be a super achiever |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-17 : 23:55:24
|
| [code]SELECT IDFROM YourTableWHERE GroupID IN (1,2,3)GROUP BY IDHAVING COUNT(DISTINCT GroupID)=3[/code] |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2008-12-18 : 00:03:54
|
| select id from tabk group by id HAVING COUNT(DISTINCT GroupID)= 3not required where condition u have check the count in having clause |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-18 : 00:05:39
|
quote: Originally posted by bklr select id from tabk group by id HAVING COUNT(DISTINCT GroupID)= 3not required where condition u have check the count in having clause
what happens when you have other groups also present? |
 |
|
|
vijayakumar_svk
Yak Posting Veteran
50 Posts |
Posted - 2008-12-18 : 00:30:35
|
| Thanks visakh16... I tried the below one but yours looks fine.select id,count(*) from (select distinct id,id1 from x) as awhere id1 in (1,2,3)group by id having count(*)=3==============================Work smarter not harder take control of your life be a super achiever |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-18 : 01:07:36
|
welcome |
 |
|
|
|
|
|