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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:46:53+00:00 2026-05-13T08:46:53+00:00

Sorry about the title, not sure how to describe without example. I trying to

  • 0

Sorry about the title, not sure how to describe without example. I trying to implement faceting of attributes in SQL Server 2008.

I have 2 tables. itemAttributes and facetParameters

Assume the following values in itemAttributes

id,   itemId,   name,      value
---------------------------------------
1     1         keywords   example1
2     1         keywords   example2
3     2         color      red
4     2         keywords   example1
5     2         keywords   example2
6     3         keywords   example2
7     3         color      red
8     3         color      blue

Assume the following values in facetParameters

name      value
----------------------
keywords  example1
color     red

I need to retrieve the (optional: distinct) itemIds where a given itemId has rows that contain all the values in facetParameters.

e.g. given the rows in facetParameters the query should return itemId 2. At the moment I would be using this in a CTE however given that they do not support a number of features I can work around this if there is no solution that works inside a CTE.

I have done a fair bit of sql over the years but this one has really stumped me and the shame is I keep thinking the answer must be simple.

  • 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-13T08:46:53+00:00Added an answer on May 13, 2026 at 8:46 am

    You could join both tables, and use a having clause to ensure that all items match:

    select     ia.itemid
    from       @itemAttributes ia
    inner join @facetParameters fp
    on         ia.name = fp.name
               and ia.value = fp.value
    group by   ia.itemid
    having     count(distinct fp.name) = 
               (
               select count(*) from @facetParameters
               )
    

    The count in the having clause assumes that the name uniquely identifies a row in the facetParameters table. If it doesn’t, add an identity column to facetParameters, and use count(distinct id_column) instead of count(distinct fp.name).

    Here’s code to create the data set in the question:

    declare @itemAttributes table (id int, itemId int, 
        name varchar(max), value varchar(max))
    insert into @itemAttributes
    select 1,1,'keywords','example1'
    union all select 2,1,'keywords','example2'
    union all select 3,2,'color','red'
    union all select 4,2,'keywords','example1'
    union all select 5,2,'keywords','example2'
    union all select 6,3,'keywords','example2'
    union all select 7,3,'color','red'
    union all select 8,3,'color','blue'
    
    declare @facetParameters table (name varchar(max), value varchar(max))
    insert into @facetParameters
    select 'keywords','example1'
    union all select 'color','red'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry about the vague subject but I couldn't think what to put. Here's my
Sorry for this not being a real question, but Sometime back i remember seeing
Sorry the title isn't more help. I have a database of media-file URLs that
Sorry for the second newbie question, I'm a developer not a sysadmin so this
Sorry if the title is poorly descriptive, but I can't do better right now
Sorry for the long question title. I guess I'm on to a loser on
Sorry for the basic question - I'm a .NET developer and don't have much
Sorry, I'm new to SVN and I looked around a little for this. How
Sorry for my ignorance here, but when I hear the word webserver, I immediately
Sorry if this sounds like a really stupid question, but I need to make

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.