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)
 How to exclude records that have a common column

Author  Topic 

JeffS23
Posting Yak Master

212 Posts

Posted - 2007-12-26 : 14:08:31
I am creating a query which will show patients that are enrolled in more than one program, but I need to exclude those patients that enrolled more than once in the same program. Here's part of the code:

SELECT member_id, service_id
FROM
pat_prg_info ppi

This query produces results like the following:

member_id service_id
1001 1
1001 2
1003 9
1003 9
1004 2
1004 9

I would like to exclude 1003, since this member_id is enrolled twice in service_id 9. How can I accomplish this?

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2007-12-26 : 14:25:15
SELECT member_id, service_id
FROM pat_prg_info ppi
GROUP BY member_id, service_id
HAVING COUNT(*) = 1

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/
Go to Top of Page
   

- Advertisement -