I have a table ALPHA with 2 fields GroupId,Member:
GroupId | Member;
A1----------A;
A1----------B;
A1----------C;
A2----------A;
A2----------B;
A3----------A;
A3----------D;
A3----------E;
Objective: Given the input of – A,B,C – I have to query the table to find if a GroupId exists for this exact set of members. So, this is what I plan to do:
- Query the table for all GroupIds whose count is 3 (since my inpt is A,B,C ..I knw its 3)
- This will give me A1,A3. Now, query this set for exact matching Member values..which will give me A1.
I plan to write a Stored Procedure and would achieve the objective somehow. But, my question can this be achieved in a single query…a single self-join perhaps.
Clarification: The set of (A,B,C) is unique to A1. And if give an input of (A,B,C,D) the query should NOT return A1.
Answers given so far assume that the Member field is unique for any given GroupID. In work I have done this isn’t the case. And also if the group has what you’re looking for, plus some extra, you need to exlucde that group.
You can also replace the IN clause with a join on to a table holding the members you are searching for…