If this applies to canceled emails, you can try EXCEPT:
SELECT Email FROM dbo.sales WHERE Status = 'Canceled' EXCEPT SELECT Email FROM dbo.sales WHERE Status = 'Approved' ;
or, if Status should be included in the output:
SELECT Status, Email FROM dbo.sales WHERE Status = 'Canceled' EXCEPT SELECT 'Canceled', Email FROM dbo.sales WHERE Status = 'Approved' ;
If you were actually intended to display emails that were always approved or always canceled, one way would be to repeat the above logic for each subset:
( SELECT Status, Email FROM dbo.sales WHERE Status = 'Canceled' EXCEPT SELECT 'Canceled', Email FROM dbo.sales WHERE Status = 'Approved' ) UNION ALL ( SELECT Status, Email FROM dbo.sales WHERE Status = 'Approved' EXCEPT SELECT 'Approved', Email FROM dbo.sales WHERE Status = 'Canceled' ) ;
Alternatively, however, you can try grouping and counting different Status values ββin groups:
SELECT MIN(Status) AS Status, Email FROM dbo.sales GROUP BY Email HAVING COUNT(DISTINCT Status) = 1 ;
This other method will work for any number of possible Status es.
source share