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)
 Tuff Sql query

Author  Topic 

itay
Starting Member

6 Posts

Posted - 2011-03-06 : 04:38:26
Hello,
I have a table with these columns : PersonID, FirstName, LastName.
I need to get all the PersonID's that have all these last names: 'aaa', 'bbb', 'ccc'.

The table looks like this:

PersonId LastName FirstName
1 aaa ddd
1 bbb fff
1 ccc eeee

Thanks.



malpashaa
Constraint Violating Yak Guru

264 Posts

Posted - 2011-03-06 : 05:14:45
Try this:

SELECT PersonId
FROM TableName
WHERE LastName IN('aaa', 'bbb', 'ccc')
GROUP BY PersonId
HAVING COUNT(DISTINCT LastName) = 3;
Go to Top of Page

itay
Starting Member

6 Posts

Posted - 2011-03-06 : 06:16:04
Perfect. Thanks.
Go to Top of Page

malpashaa
Constraint Violating Yak Guru

264 Posts

Posted - 2011-03-06 : 06:47:16
Welcome.
Go to Top of Page
   

- Advertisement -