I am trying to restrict the results returned from a query. If a user has access to more Entities than just those listed for the current user, then that user should not show up on the list. Using the data below, and assuming User 1 is running the query, since the user with UserId 2 has matches that User 1 does not have, even though they have overlapping values, user 2 should be excluded from the results of the query.
Table1
UserId EntityId
1 100
1 101
1 102
2 100
2 101
2 102
2 200
2 201
How do I do this?
You could do this with a series of nested queries:
The bottom, innermost query gives us the EntityIds belonging to UserId 1. We use that list in the next query up to find all the UserIds which have an EntityId NOT in this list. Armed with that list of UserIds we don’t want, the outermost query dumps all the rows for the remaining UserIds. These will be ones which have a subset of UserId 1’s set of EntityIds