My data table looks like this:
client Status 1 a 2 b 3 a 4 d 5 d 6 a 7 e 8 b 9 e 10 f
Say I want to group by the table by status, but instead, I want group status a,b,d (status I) as one group and e,f (status II) as another group
I want it looks like this eventually
status Count I 7 II 3
How should I write the query?
You can run the grouping and count into one statement.
SELECT
CASE WHEN Status IN ('a', 'b', 'd') THEN 'I' ELSE 'II' END AS Status,
count(*) as count
FROM Table1
GROUP BY (CASE WHEN Status IN ('a', 'b', 'd') THEN 'I' ELSE 'II' END)
ORDER BY Status
Result:
Status count
I 7
II 3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With