patternMinor
Find rows matching only values from a specific list with SQL
Viewed 0 times
rowswithsqlspecificfindvalueslistfromonlymatching
Problem
I have a table like this:
I want to show rows that have the
So, for my example I would like the output to be this:
What should my query look like?
Id Status
-- -----------
1 Closed
1 In progress
2 Rejected
3 Cancelled
4 Rejected
4 Closed
5 In progressI want to show rows that have the
Status of closed, rejected or cancelled. However, I want to exclude the IDs that have a different status value, even if they also have any of those three.So, for my example I would like the output to be this:
Id Status
-- ---------
2 Rejected
3 Cancelled
4 Rejected
4 ClosedWhat should my query look like?
Solution
One way would be to use
Or an alternative method if your DBMS supports windowed aggregates..
And another option using
NOT EXISTS.SELECT *
FROM YourTable T1
WHERE Status IN ( 'Closed', 'Rejected', 'Cancelled' )
AND NOT EXISTS (SELECT *
FROM YourTable T2
WHERE T2.Id = T1.Id
AND T2.Status NOT IN ( 'Closed', 'Rejected', 'Cancelled' )) ;Or an alternative method if your DBMS supports windowed aggregates..
SELECT Id,
Status
FROM (SELECT *,
COUNT(CASE WHEN Status NOT IN ('Closed','Rejected','Cancelled')
THEN 1 END)
OVER (PARTITION BY Id) C
FROM YourTable) D
WHERE Status IN ( 'Closed', 'Rejected', 'Cancelled' )
AND C = 0 ;And another option using
EXCEPT:SELECT *
FROM YourTable
WHERE Id IN
( SELECT Id FROM YourTable
WHERE Status IN ( 'Closed', 'Rejected', 'Cancelled' )
EXCEPT
SELECT Id FROM YourTable
WHERE Status NOT IN ( 'Closed', 'Rejected', 'Cancelled' )
) ;Code Snippets
SELECT *
FROM YourTable T1
WHERE Status IN ( 'Closed', 'Rejected', 'Cancelled' )
AND NOT EXISTS (SELECT *
FROM YourTable T2
WHERE T2.Id = T1.Id
AND T2.Status NOT IN ( 'Closed', 'Rejected', 'Cancelled' )) ;SELECT Id,
Status
FROM (SELECT *,
COUNT(CASE WHEN Status NOT IN ('Closed','Rejected','Cancelled')
THEN 1 END)
OVER (PARTITION BY Id) C
FROM YourTable) D
WHERE Status IN ( 'Closed', 'Rejected', 'Cancelled' )
AND C = 0 ;SELECT *
FROM YourTable
WHERE Id IN
( SELECT Id FROM YourTable
WHERE Status IN ( 'Closed', 'Rejected', 'Cancelled' )
EXCEPT
SELECT Id FROM YourTable
WHERE Status NOT IN ( 'Closed', 'Rejected', 'Cancelled' )
) ;Context
StackExchange Database Administrators Q#140922, answer score: 8
Revisions (0)
No revisions yet.