I have separate sets of "classes" and "groups", each of which is assigned one or more tags. I would like to find for each group a subset of classes that contains the same (or more) tags for each group.
Some sample data:
declare @Groups table ( GroupID int, TagID int ) insert @Groups values (1,1),(1,2),(1,3), (2,1),(2,2), (3,1),(3,2),(3,3),(3,4) declare @Classes table ( ClassID int, TagID int ) insert @Classes values (1,1),(1,2), (2,1),(2,2), (3,1),(3,2),(3,3) select * from @Groups select * from @Classes
And the conclusion:
GroupID TagID 1 1 1 2 1 3 2 1 2 2 3 1 3 2 3 3 3 4 ClassID TagID 1 1 1 2 2 1 2 2 3 1 3 2 3 3
An example set of results would look like this:
declare @Results table ( GroupID int, ClassID int ) insert @Results values (1,3),(2,1),(2,2),(2,3),(3,null) select * from @Results
Output result:
GroupID ClassID 1 3 2 1 2 2 2 3 3 NULL
I understand that this is a type of relational division problem, including having and count . These posts describe what I want to do, but I cannot figure out how to apply the examples to the specific case above: