我有一个简单的表格,其中包括卖家,买家,交易日期和总计。
我想在给定年份按季度对每个卖家的交易进行汇总。季度从6月1日开始,到5月31日结束。
我知道卖家在给定的日期范围内如何求和,但无法弄清楚如何在多个季度内完成。
我希望它看起来像
卖家Q1Total Q2Total Q3Total Q4Total
看起来似乎应该相当简单,但我很难弄清楚。
答案 0 :(得分:0)
您可以使用条件聚合执行此操作:
select Seller,
sum(case when Transactiondate between '20150601' and '20150831' then Amount else 0 end) as Q1Amount,
sum(case when Transactiondate between '20150901' and '20151231' then Amount else 0 end) as Q2Amount,
sum(case when Transactiondate between '20160101' and '20160229' then Amount else 0 end) as Q3Amount,
sum(case when Transactiondate between '20160301' and '20160531' then Amount else 0 end) as Q4Amount
from TableName
group by Seller