Given a set of product_ids
, what are the order_ids
that only have those product_ids
?
For the example below, I'm only wanting order_ids that have some combination of (a,b,c)
. I have 2 tables like below:
"transactions" table:
order_id | product_id |
---------+-------------
1 | a |
1 | b |
2 | a |
2 | X |
3 | a |
3 | b |
3 | c |
... | ... |
999 | Y |
"products" table:
product_id |
------------
a |
b |
c |
d |
X |
Y |
... |
ZZZ |
Desired Output has 2 order_ids with expected table output:
order_id |
----------
1 |
3 |
Notice that order_id == 2
is removed although it has product_id == a
but because it has product_id == X
then it should be removed.
Therefore it's not a simple:
SELECT DISTINCT(order_id)
FROM transactions
WHERE product_id IN (a, b, c)
distinct
is not a function. It always applies to all columns in the select list. Enclosing one of the columns with parentheses won't change anything and is useless.distinct (a),b
is the same asdistinct a,(b)
ordistinct a,b