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 2000 Forums
 SQL Server Development (2000)
 Match Same criteria on certain groups

Author  Topic 

db_sysadmin
Starting Member

27 Posts

Posted - 2006-05-10 : 10:21:40
Hey Guys!

I need to check where all of the relatives of an employee matches some certain criteria all at the same time...

Example:

Employee_ID....Relative_ID.....Rel_Name....Rel_Sex
...1...............1............John.........M
...1...............2............Mary.........F
...1...............3............Hank.........M
...2...............1............Lucy.........F
...2...............2............Jane.........F
...2...............3............Julie........F
...3...............1............Sandra.......F
...3...............2............Mark.........M
...4...............1............Willie.......M

The query I want to perform would be like:

Employee_ID where all relatives have Rel_Sex = Female (F)

The result of this query would be:

Employee_ID
-----------
....2

Whats the way or best way to do it?

Thanks a lot guys!

sshelper
Posting Yak Master

216 Posts

Posted - 2006-05-10 : 11:39:33
Try this:

SELECT Employee_ID, COUNT(*), SUM(CASE WHEN Rel_Sex = 'F' THEN 1 ELSE 0 END)
FROM Employees
GROUP BY Employee_ID
HAVING COUNT(*) = SUM(CASE WHEN Rel_Sex = 'F' THEN 1 ELSE 0 END)

SQL Server Helper
http://www.sql-server-helper.com
Go to Top of Page
   

- Advertisement -