Let me elaborate with examples.
I have three tables Entity(EntityId int), Group(GroupId int), and Mapping(MapId int, GroupId int, EntityId int).
Let's take a few rows of data.
Entity
EntityId
1
2
3
4
Group
GroupId
101
102
103
Mapping
MapId GroupId EntityId
1 101 2
2 102 1
3 102 3
4 103 3
I need to select entities that are not used in at least one input group or are not used in any non-input groups.
Input-GroupId Output-Entities
101 1,3,4 ( 2 is mapped with 101)
101,102 1,2,3,4( 1 is mapped with 102 but not with 101 and 2 is mapped with 101 but not with 102)
102,103 1,2,4 ( 3 is mapped with both 102 and 103 so it will not be in output.)
If there are two input groups. According to the below ven diagram, I need values of all cross-red line areas.
I tried a few join queries but I am able to get only values that are not used in any groups.
Edit 1:-
SELECT distinct EntityId from Entity e
JOIN Mapping map ON e.EntityId != map.EntityId
JOIN #temp t ON map.GroupId = t.GroupId order by EntityId
I have tried this query. It works fine for Input-Groups- (101), (101, 102) but does not work for the case (102, 103).
Edit 2:- A few comments mentioned that the ven diagram is not clear. So this edit.
Let's consider, A(101) and B(102), two groups are provided as input.
then all red dotted areas will be considered for the output. The blue pen highlighted area will not be considered.