Question : Query to find matching patterns in a table

I'm looking to construct a query that would pull matching groups of record from a table. Specifically, the table is a list of items ordered by customers in an e-commerce site. What I'm trying to find is all of the customers who ordered a specific group of products.

Here's a simplified version of my table structure:

OrderNumber int
CartItemID int
CustomerID int
ProductID int
Quantity int

Simply put, I want to pull a list of customers who ordered ProductID 31, Product ID 61 (twice) Product ID 59, Product ID 60, and Product ID 9999 all within the same order. It doesn't matter if they ordered items in addition to these.

Answer : Query to find matching patterns in a table

this is the list of such customers (assuming you have customers table)

1:
2:
3:
4:
5:
6:
7:
8:
9:
SELECT   *
  FROM   customers c,
         (  SELECT   DISTINCT customerid
              FROM   orders
             WHERE   productid IN (31, 59, 60)
                     OR (productid = 61 AND Quantity = 2)
          GROUP BY   orderNumber
            HAVING   COUNT ( * ) = 4) x
 WHERE   c.customerid = x.customerid
Random Solutions  
 
programming4us programming4us