如何在SQL中以Pivot格式获取月度报告?

时间:2019-04-13 20:12:13

标签: sql sql-server tsql

如何在SQL中以Pivot格式获取月度报告?

我正在尝试生成..

中的月度报告的枢轴格式。

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以使用conditional aggregation

select SourceID,   
       sum(case when month(FileReceivedDate)= 1 then 1 else 0 end ) as Jan,
       sum(case when month(FileReceivedDate)= 2 then 1 else 0 end ) as Feb,
       sum(case when month(FileReceivedDate)= 3 then 1 else 0 end ) as Mar,
       sum(case when month(FileReceivedDate)= 4 then 1 else 0 end ) as Apr
   from tab
  group by SourceId