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 1003599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:00:18+00:00 2026-05-16T08:00:18+00:00

I asked this question earlier, but I changed the table structure based on recommendations.

  • 0

I asked this question earlier, but I changed the table structure based on recommendations. So, here’s the question with the new tables and some additional requirements. I have the following tables:

Products
ID BIGINT
Name VARCHAR(64)
Category1_ID INT
Category2_ID INT
Category3_ID INT

Categories
ID BIGINT
Name VARCHAR(64)

Product_Keywords
PRODUCT_ID BIGINT (FK to Products)
Keyword VARCHAR(64)

User_Interests
ID BIGINT
USER_ID BIGINT (FK TO Users)
Category_ID INT

Accepted_Keywords
USER_INTEREST_ID BIGINT (FK to User_Interests)
Keyword VARCHAR(64)

Blocked_Keywords
USER_INTEREST_ID BIGINT (FK to User_Interests)
Keyword VARCHAR(64)

A product (Products table) can belong to 1 to 3 categories (at least one) – so Category1 is always specified, but Category2 and Category3 may or may not be null. A product can have 0 or more keywords (Product_Keywords).

A user can specify one or more categories of interest (User_Interests). Each category of interest can have zero or more accepted keywords (Accepted_Keywords). It can also have zero or more blocked keywords (Blocked_Keywords).

I would like to know the products of interest to a given user, based on categories matched in addition to the following rules:
1. If there are NO Accept or Block keywords, then category match is sufficient
2. If Accept has keywords, then there should be at least 1 keyword match between Accept & Product_Keywords
3. If Block has keywords, then there should be no matches between Block & Product_Keywords
4. Combination of 2 & 3

Ideally I’d like to do this in a SQL query. I’m open to stored procedures either Managed or T-SQL in that order. I’m using SQL Server 2008, C# & .NET 4.0.

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-16T08:00:18+00:00Added an answer on May 16, 2026 at 8:00 am
    CREATE PROCEDURE GetProductsOfInterest
        @USER_ID BIGINT NOT NULL
    AS
    SET NOCOUNT ON
    
    SELECT p.ID, p.Name
    FROM   Products p
    INNER JOIN User_Interests ui
    ON     ui.USER_ID = @USER_ID
    AND    (ui.Category_ID = p.Category1_ID OR
            ui.Category_ID = p.Category2_ID OR
            ui.Category_ID = p.Category3_ID)
    WHERE  (NOT EXISTS (SELECT 1 FROM Product_Keywords pk1
                WHERE  pk1.PRODUCT_ID = p.ID
                AND    EXISTS (SELECT 1 FROM Blocked_Keywords bk1
                       WHERE  bk1.USER_INTEREST_ID = ui.ID
                       AND    bk1.Keyword = pk1.Keyword)) AND
        (NOT EXISTS (SELECT 1 FROM Accepted_Keywords ak1
                 WHERE  ak1.USER_INTEREST_ID = ui.ID) OR
         EXISTS (SELECT 1 FROM Product_Keywords pk2
             WHERE  pk2.PRODUCT_ID = p.ID
             AND    EXISTS (SELECT 1 FROM Accepted_Keywords ak2
                        WHERE  ak2.USER_INTEREST_ID = ui.ID
                    AND    ak2.Keyword = pk2.Keyword)))
    GO
    

    I can’t promise this will work completely, given no good way to debug at the moment. This code assumes that if a keyword appears in both the Accepted and Blocked lists for a given interest, the Blocked list prevails. Also, the Accepted and Blocked lists apply only to one specific interest category for a customer. Finally, if an item is in more than one category of interest, it could show up once per category of interest, subject to the accepted/blocked rules for each category of interest.

    Typically, I would look at an execution plan to see where improvements might be warranted as well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I asked this question earlier: How to get delta between two text items But
I asked this question earlier. I am intrigued by std::set but I have another
I asked another version of this question on the gamedev.SE site earlier today but
This question was asked earlier by someone else but never answered: MKAnnotation - Map
I asked this question earlier, but it got a negative vote, so I'm rewording
I've asked about this earlier but the question itself and all the information in
I asked this question earlier but I was very vague and a lot of
I know that this question was asked earlier but the OP didn't get any
Based on this question I asked earlier on setting up cookies in Perl ,
I asked this question earlier but regarding another programming languages. Let's say I have

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.