SELECT
MemberName
FROM
Groups g
INNER JOIN (VALUES ('Group1'),('Group2')) v(groupname) ON
v.groupname = g.groupname
GROUP BY
g.MemberName
HAVING
COUNT(DISTINCT g.groupname) = 2;
-- 2 is the number of groups in which membership is requried.
-- i.e., the groups listed in the values clause above.
Thank you for your reply. I have problem running this query, I get syntax error for the VALUES statement. How can you join the two tables using groupname when it only exists in on of the tables?
What is the error you are getting? What version of SQL Server are you running (SELECT @@VERSION will tell you). You can modify the query like shown below to avoid values clause
SELECT
MemberName
FROM
Groups g
WHERE
g.GroupName IN ('Group1','Group2')
GROUP BY
g.MemberName
HAVING
COUNT(DISTINCT g.groupname) = 2;
Thanks again. I should have told you that I'm using a SQL CE 4 database. The first part of your script is working, but it is trowing an error on the HAVING part of the script. The DISTINCT function is working for CE, but not in the same way. Not sure how to use it together with =2 in this case.