Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7431561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:18:54+00:00 2026-05-29T09:18:54+00:00

I have a table which contains the item and category ids: create table SomeTable

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T09:18:55+00:00Added an answer on May 29, 2026 at 9:18 am

    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.)

    set nocount on;
    
    -- create a sample table
    declare @T table ( ItemId int identity(1,1), CategoryId int );
    insert @T values ( 100 );
    insert @T values ( 100 );
    insert @T values ( 100 );
    insert @T values ( 100 );
    insert @T values ( 100 );
    insert @T values ( 200 );
    insert @T values ( 200 );
    insert @T values ( 300 );
    insert @T values ( 300 );
    insert @T values ( 300 );
    insert @T values ( 300 );
    insert @T values ( 500 );
    insert @T values ( 500 );
    insert @T values ( 500 );
    insert @T values ( 600 );
    insert @T values ( 700 );
    insert @T values ( 800 );
    insert @T values ( 800 );
    insert @T values ( 800 );
    insert @T values ( 900 );
    
    
    -- grab some CategoryIDs to work with
    declare @X table ( CategoryId int );
    insert @X
        select CategoryID=200 union
        select CategoryID=400 union
        select CategoryID=600 union
        select CategoryID=800 
    
    
    -- A. Number of category ids of item id that are not in set x
    select distinct t.CategoryID from @T t
    where not exists(select 1 from @X x where t.CategoryID = x.CategoryID)
    
    -- or, using the set difference operator
    select CategoryID from @T
    except
    select CategoryID from @X
    
    
    -- B. Number of category ids shared between item id and set x
    select distinct x.CategoryID from @X x
    join @T t on t.CategoryID = x.CategoryID;
    
    -- or, using set intersection
    select CategoryID from @T
    intersect
    select CategoryID from @X
    
    
    -- C. Number of category ids in set x but which are not associated with item id 
    select distinct x.CategoryID from @X x
    where not exists(select 1 from @T t where t.CategoryID = x.CategoryID)
    
    -- or, using the set difference operator
    select CategoryID from @X
    except
    select CategoryID from @T
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a database (NexusDB (supposedly SQL-92 compliant)) which contains and Item table, a
I have a table which contains my server status create table ServerStatus ( ServerId
Ok, I have a table which contains multiple rows. Each row contains some data
I have a table which contains delete buttons for each row. I use int
I have a ListView which contains EMPLOYEE_NAME from a DB table EMPLOYEE. Some employees
I have a mysql table which contains is: folder_id (int(11), auto increment) folder_name (varchar)
I am having some redundant primary key issues. I have an Item which contains
I have a table which contains my ads that can be searched in sql-server-2008.
i have a table which contains a bunch of dynamically created radio button lists,
I have a table which contains house details called property. I am creating a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.