0

I have a very simple item ownership table with these two columns:

UserID, ItemID

There is an index on UserID, but not ItemID.

I have a set S of 10-40 specific ItemIDs (in my queries they are just a comma delimited list of integers).

I want to find all UserIDs that own at least X (distinct ItemIDs) of the items in S.

I am using MSSQL if it matters. Can this be done efficiently?

John Shedletsky
  • 7,110
  • 12
  • 38
  • 63

1 Answers1

3
select UserID
from Ownership 
where ItemID in (1,2,3,4,5,...) --your list of ItemIDs
group by UserID
having count(distinct ItemID) >= 3 --the minimum # of distinct items required
D'Arcy Rittich
  • 167,292
  • 40
  • 290
  • 283