I have a table which contains the item and category ids:
create table SomeTable (
ItemId int,
CategoryId int
)
Given some category ids (Set X) I would like to determine all item ids that share at least one category id and some stats for each of these item ids:
A – Number of category ids of item id that are not in set x
B – Number of category ids shared between item id and set x
C – Number of category ids in set x but which are not associated with item id
I have written some tsql code which involves a cross join and several ctes plus left joins. It works but is fairly slow.
I am sure someone must have encountered a similar problem. I would provide the code but the above description is simplified. Thanks.
Here’s a couple of ideas. (I don’t know how they’ll compare performance wise with what you have already. Left for you to benchmark.)