I have a table in which transactions are written. I want to select all identifiers that have exactly 4 transactions worth 2000, but then exclude those that have a single transaction 2500.
SELECT t.tuserid, COUNT(*) AS CNT
FROM transactions t
WHERE t.amt = 2000
GROUP BY t.tuserid
HAVING CNT = 4;
This part is simple enough, but I'm not sure how to effectively exclude anyone who has a transaction with t.amt = 2500. Would the simplest request for a where clause be the most efficient?
SELECT t.tuserid, COUNT(*) AS CNT
FROM transactions t
WHERE t.amt = 2000
AND t.tuserid NOT IN (SELECT x.tuserid FROM transactions x WHERE x.amt=2500)
GROUP BY t.tuserid
HAVING CNT = 4;
The transaction table is large, and I'm not sure if a subquery is the most efficient way to start this process.
Steve source
share