Avatar of Neil Russell
Neil Russell
Flag for United Kingdom of Great Britain and Northern Ireland asked on

SQL Query using OR now want AND alternative.

I have the following SQL query that works just fine where I want all PARTS that have ANY of the selected Categories.

SELECT  DISTINCT * from PARTS where Id in (Select FKPARTS from PartsCategories WHERE FKCategory in (12,22,33))

Open in new window


What I want now is the equivalent to select ONLY those PARTS where they have a Category of 12 AND 22 AND 33

Most compact and easiest bearing in mind that the select statement is built up dynamically based on a selection of Categories in a front end app with tick boxes.
Microsoft SQL ServerMicrosoft SQL Server 2008Databases

Avatar of undefined
Last Comment
Neil Russell

8/22/2022 - Mon
SOLUTION
Jim Horn

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Neil Russell

ASKER
The relationship is that Each PART can have any number of Categories. The join table is PartsCategories
that has an Id, FKCategory and FKParts, respectively the Key of a category and a part.

So if I say I want Parts for Categories 12,22 and 33 then yes, all 3 rows must exist in PartsCategories for a Part to be included in the result set.

If i had Categories of 12 and 22 then of course all Parts with those two categories would be included.

Oh and of course the Categories and not Exclusive, a part can have other categories but those are not part of the select.

Hope thats clear?
Qlemo

Do you mean "exactly all of those categories" or "at least all of those categories"? So there may be more, but not less, then the given categories?
Neil Russell

ASKER
@Qlemo
Correct, there must be ALL of the ones in question but may be others.

So if a Part had categories of 1,5,7,12,22 and 33 it would still match 12,22,33 but NOT match 7,12,23
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
ASKER CERTIFIED SOLUTION
Scott Pletcher

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
Kyle Abrahams, PMP

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Kyle Abrahams, PMP

Scott's and I are pretty close . . . you can use his but

HAVING COUNT(DISTINCT FKCategory) = 3

should be
 
HAVING COUNT(DISTINCT FKCategory) >= 3
Qlemo

Scott is correct, the subselect only asks for 3 categories, so the distinct count can only be <=3 ;-)
Kyle Abrahams, PMP

Yeah . . . brain is fried . . . oversight on my part.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Neil Russell

ASKER
Yes Scotts works perfectly with =3
Bang on and simple :D
Neil Russell

ASKER
Simple and easy to understand! As are all things once you know the answer!