I have a table that consists of the following:
|---------------------|------------------|
| CustomerID | Product ID |
|---------------------|------------------|
| 1 | 101 |
|---------------------|------------------|
| 1 | 102 |
|---------------------|------------------|
| 1 | 103 |
|---------------------|------------------|
| 2 | 101 |
|---------------------|------------------|
| 2 | 105 |
|---------------------|------------------|
I only want to select unique Customer IDs where they have exactly the product IDs of 101, 102, 103. I don't want to pull in customer with ID 2 because, although they have Product ID 101, they don't have the correct combination.
This should work:
SELECT CustomerID
FROM myTables
WHERE ProductID IN(101,102,103)
GROUP BY CustomerID
HAVING COUNT(DISTINCT ProductID) = 3
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments