Tricky Select Statement...

Hoping for some help writing a query ...

I have a Products Table and it has a Child table "Colors."   Let's say the Product is a Box and the Colors are Red, Green and Blue.    I have an Criteria table  "MatchColors"  which may contain all or a subset of those colors.

I need to fetch a list of products that have *every* color matched.   That is, take a Product and for each Color record there is a matching MatchColor Record.   I need to return a list of products where ALL their colors are found the in MatchColor table.   I am having trouble because I am returning records that match at least one product, but I have to ensure that every color has a matching record.  

 Colors     ---        MatchColors
  colorID (pkey)    matchColorID (pkey)
  colorName         colorID  (column to match up)
LVL 39
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott PletcherSenior DBACommented:
You haven't really shown how colors are related to products.  Is there a ProductColors table?!
At any rate, the general approach is as follows:

SELECT p.ProductID
FROM Products p
INNER JOIN MatchColors mc ON mc.colorID = p.ColorID
GROUP BY p.ProductID
--ORDER BY ProductID
gdemariaAuthor Commented:
Thanks Scott, I was just editing my question and by the time I hit submit, I was no longer allowed too.  You are too fast!

The Color Record has a ProductID, simple foreign key relationship.

The other statement that I missed is that there must be at least one color record.  Some products have no color records.  I do not want these returned.  

I will examine your query to see how it works for me, thanks very much
Scott PletcherSenior DBACommented:
Sorry :-).  Hmm, as an aside, the table name really should be "ProductColors" then, to show that it is an intersection table, with a separate "master" colors table of Colors.

SELECT c.ProductID
FROM Colors c
INNER JOIN MatchColors mc ON mc.colorID = c.ColorID
GROUP BY c.ProductID
--ORDER BY ProductID

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
HuaMin ChenProblem resolverCommented:
what you need really depends on the data of the tables.

But you can have a temp table for achieving that. You use cursor to retrieve records from the tables to temp table. while in your loop, you still have the record to every color, even if there is no record retrieved per given color.
gdemariaAuthor Commented:
Thanks Scott !   It became a little more complex, but using the idea of comparing counts was certainly the key for the solution!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.