SQL:按日期对结果进行分组

时间:2014-07-16 17:51:31

标签: sql postgresql

我有一张桌子:

EventTime         | InTeam    | Points |
----------------------------------------
2014-07-16 11:40      True       10
2014-07-16 10:00     False       20
2014-07-16 09:20      True       30
2014-07-15 11:20     False        5
2014-07-15 10:20      True       10
2014-07-15 09:00     False       10

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

EventDate   | InTeam Points | Not In Team Point |
-------------------------------------------------
2014-07-16                40                  20
2014-07-15                10                  15

InTeam Points是当天所有团队积分的总和,Not In Team Point是当天不在团队积分中的总和?

1 个答案:

答案 0 :(得分:5)

select date(eventtime) as eventtime,
       sum(case when inteam = 'true' then points end) as in_team_points,
       sum(case when inteam = 'false' then points end) as not_in_team_points
from your_table
group by date(eventtime)