PostgreSQL 9.4。
PostgreSQL 9.4
我有下表:
id player_id serial PK integer --------------------------- 1 1 2 3 ... ... 123123 1
我需要用计算所有行player_id = 1。可能与COUNT聚合有关吗?
player_id = 1
COUNT
现在,我按如下操作:
SUM(CASE WHEN player_id = 1 THEN 1 ELSE 0 END)
如果您只需要对player_id1的行数进行计数,则可以执行以下操作:
player_id
SELECT count(*) FROM your_table_name WHERE player_id = 1;
如果要计算 每 行的行数player_id,则需要使用GROUP BY:
GROUP BY
SELECT player_id, count(*) FROM your_table_name GROUP BY player_id;