I have a table called PM_BATCH_ST
like this:
BATCH_ID | RESULT |
---|---|
1000564932 | ISC |
1000564932 | LLC |
1000585739 | ISC |
1000585739 | LLC |
1000384769 | ISC |
1000384769 | LLC |
1000384757 | LLC |
1000888940 | ISC |
Can I filter the table to obtain BATCH_ID
s that ONLY have an ISC
entry? So the result I would like is just 1000888940.
I understand that I can filter it by
WHERE RESULT LIKE '%ISC%'
But that will also return 1000564932, 1000585739, 1000384769 which also have an entry for LLC
.
You can select like this
SELECT BATCH_ID
FROM PM_BATCH_ST
GROUP BATCH_ID
HAVING COUNT(*) = 1 AND MAX(RESULT) = 'ISC'
Unlike the WHERE clause the HAVING clause is exceuted on the grouped result. COUNT(*) = 1
ensures there is only one result and MAX(RESULT) = 'ISC'
ensures this reult is for a ISC. I took MAX
because we need an aggregate function here, but since we have only one result MIN
would work as well.