I have the following data
id | sub_id |status |
---|--------|-------|
1 | 1 | new |
2 | 2 | old |
3 | 2 | new |
4 | 3 | old |
Which query should I use to get the following result?
I want to group the result by sub_id and then add new columns that store the number of statuses of the corresponding sub_id.
sub_id | new | old | total |
-------|------|------|-------|
1 | 1 | 0 | 1 |
2 | 1 | 1 | 2 |
3 | 0 | 1 | 1 |
I tried this and it did not work as expected.
SELECT
sub_id,
count(status='new') AS new,
count(status='old') AS old,
count(status) AS total
FROM table
GROUP BY sub_id;
count(*) filter (where status = 'new')