PostgreSQL 9.4
.
I have the following table:
id player_id
serial PK integer
---------------------------
1 1
2 3
... ...
123123 1
I need to count all rows with player_id = 1
. Is it possible to do with the COUNT
aggregate?
Now I do it as follows:
SUM(CASE WHEN player_id = 1 THEN 1 ELSE 0 END)
If all you need is a count of the number of rows where player_id
is 1, then you can do this:
SELECT count(*)
FROM your_table_name
WHERE player_id = 1;
If you want to count the number of rows for each player_id
, then you will need to use a GROUP BY
:
SELECT player_id, count(*)
FROM your_table_name
GROUP BY player_id;
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