CUST ID | ORDER ID |
---|---|
12455 | 4070 |
12454 | 4070 |
12454 | 4077 |
12455 | 4077 |
12454 | 2659 |
12455 | 1928 |
12454 | 2012 |
12453 | 1354 |
I have a table that holds both customer id and order id. I need to find different customer id's with the same order id which the output should look like below. In this case 12455 and 12454 matches on same order id 4070, 4077.
What i need is a query like this if i select
select * from table where cust_id in (12455, 12454) ... query continues
the output should like below because 4070 and 4077 are common in given cust id's
ORDER ID |
---|
4070 |
4077 |
if i select for select * from table where cust_id in (12454,12455,12453) ... query continues
output should be empty.
Your logic is not completely clear, but it seems to be a classic Relational Division Without Remainder problem
WITH IdsToSearch AS (
SELECT *
FROM (VALUES (12455), (12454) ) v(ID)
)
SELECT
t.ORDERID
FROM YourTable t
JOIN IdsToSearch i ON i.ID = t.cust_id
GROUP BY
t.ORDERID
HAVING COUNT(*) = (SELECT COUNT(*) FROM IdsToSearch);