If
SOURCE
values are guaranteed to be unique perCURRENT
:SELECT CURRENT FROM atable GROUP BY CURRENT HAVING COUNT(SOURCE) = 2 AND COUNT(CASE WHEN SOURCE IN ('A', 'B') THEN SOURCE END) = 2 ;
If
SOURCE
values aren't unique perCURRENT
butCURRENT
s with duplicate entries of'A'
or'B'
are allowed:SELECT CURRENT FROM atable GROUP BY CURRENT HAVING COUNT(DISTINCT SOURCE) = 2 AND COUNT(DISTINCT CASE WHEN SOURCE IN ('A', 'B') THEN SOURCE END) = 2 ;
If
SOURCE
values aren't unique and groups with duplicateSOURCE
entries aren't allowed:SELECT CURRENT FROM atable GROUP BY CURRENT HAVING COUNT(SOURCE) = 2 AND COUNT(DISTINCT SOURCE) = 2 AND COUNT(DISTINCT CASE WHEN SOURCE IN ('A', 'B') THEN SOURCE END) = 2 ;
Every query returns only distinct CURRENT
values matching the requirements. Use the query as a derived dataset and join it back to your table to get the details.
All the above options assume that either SOURCE
is a NOT NULL
column or that NULLs can just be disregarded.