Oracle SQL: GROUP BY HAVING multiple criteria

10.7k views Asked by At

I have the following query:

SELECT PERSON_ID 
FROM TABLE
WHERE YEAR > 2013
AND ACTION = 'TERM'
GROUP BY PERSON_ID
HAVING COUNT(ACTION) = 1

This query is returning PERSON_IDs with one TERM action among other actions.

How can I modify my HAVING clause to have the query return PERSON_IDs with one TERM action and no other actions? I tried moving the AND ACTION = 'TERM' below the HAVING line, but, as there is no GROUP BY operation in that line, I am getting an error.

1

There are 1 answers

1
Gordon Linoff On

Here is one method:

SELECT PERSON_ID 
FROM TABLE
WHERE YEAR > 2013
GROUP BY PERSON_ID
HAVING COUNT(ACTION) = 1 AND MIN(ACTION) = 'TERM'