SQL:按2列分组

时间:2014-07-16 19:31:57

标签: sql postgresql

我有一张表points

event_time         | name  | points |
------------------------------------
2014-07-16 11:40     Bob        10
2014-07-16 10:00     Jim        20
2014-07-16 09:20     Jim        30
2014-07-15 11:20     Bob         5
2014-07-15 10:20    Anna        10
2014-07-15 09:40     Bob        30
2014-07-15 09:00    Anna        10

是否可以使用以下结果进行查询:

event_date   | name | total_points |
------------------------------------
2014-07-16     Bob            10
2014-07-16     Jim            50
2014-07-15     Bob            35
2014-07-15    Anna            20

total_points是一天中给定名字的所有点的总和?

1 个答案:

答案 0 :(得分:2)

select date(event_time) as event_date,
       name,
       sum(points) as total_points
from points
group by date(event_time), name