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
 General SQL Server Forums
 New to SQL Server Programming
 Distinct record equal to 2 values from same column

Author  Topic 

daddyjmo
Starting Member

2 Posts

Posted - 2013-12-06 : 14:41:29
Distinct name that match both subjects (math, science) from classname in level 2 only. Not sure where to even start. Any help is greatly appreciated! Sorry for the crude example table below:


name subject level
bob math 2
hank math 1
joe science 2
bob science 2
joe math 2
ben science 2
carl science 1

daddyjmo
Starting Member

2 Posts

Posted - 2013-12-06 : 14:43:02
So basically return set should return bob and joe because they both have math and science at level 2 only.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-12-07 : 08:22:04
[code]
SELECT name
FROM table
GROUP BY name
HAVING COUNT(DISTINCT CASE WHEN subject IN ('math','science') AND level = 2 THEN subject END) =2
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -