Table:
+++++++++++++++++++++++++
+ id | event | group_id +
+ 1 | '+1' | 1 +
+ 2 | 'pt' | 1 +
+ 3 | 'pt' | 1 +
+ 4 | '+1' | 1 +
+ 5 | 'pt' | 1 +
+ 6 | '+1' | 1 +
+ 7 | 'pt' | 1 +
+ 8 | '+1' | 1 +
+++++++++++++++++++++++++
I need to select SUM(CASE WHEN event = '+1' THEN 1 ELSE 0 END)
since SUM(CASE WHEN event = 'pt' THEN 1 ELSE 0 END) = 3
.
I tried this:
SELECT group_id, SUM(CASE WHEN event = '+1' THEN 1 ELSE 0 END) AS event_sum
FROM Table
WHERE SUM(CASE WHEN event = 'pt' THEN 1 ELSE 0 END) >= 3
ORDER BY id
GROUP BY group_id
But this is not the way how group function works, so how could I achieve this output ? Do I need to use user variable to select id when condition SUM(CASE WHEN event = 'pt' THEN 1 ELSE 0 END) = 3
is true ? I believe that it is possible to do it without user variable, is it possible ?
Desired output:
++++++++++++++++++++++++
+ group_id | event_sum +
+ 1 | 2 +
++++++++++++++++++++++++