我有这样的数据:
email | ticket | date
a | 1 | 1/1/2015
a | 1 | 2/1/2015
a | 3 | 5/6/2016
b | 1 | 3/4/2016
b | 2 | 7/9/2016
b | 2 | 3/2/2016
我想通过不同的日期缩减来对门票进行汇总,并提供两种不同的潜在布局:
email | ticket | date
a | 2 | 2015
a | 3 | 2016
b | 5 | 2016
email | 2015 tickets | 2016 tickets
a | 2 | 3
b | 0 | 5
答案 0 :(得分:1)
你只需聚合。
--Query 1
select email,sum(ticket),extract(year from date)
from tablename
group by 1,3
--Query 2
select email
,sum(case when extract(year from date) = 2015 then ticket else 0 end)
,sum(case when extract(year from date) = 2016 then ticket else 0 end)
from tablename
group by email