Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在SQL中获取透视格式的月报?_Sql_Sql Server_Tsql - Fatal编程技术网

如何在SQL中获取透视格式的月报?

如何在SQL中获取透视格式的月报?,sql,sql-server,tsql,Sql,Sql Server,Tsql,如何在SQL中获取透视格式的月报 我正在尝试在..中生成月度报告的透视格式 您可以使用条件聚合 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(Fil

如何在SQL中获取透视格式的月报

我正在尝试在..中生成月度报告的透视格式


您可以使用
条件聚合

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

谢谢。。但是如果简在两个不同的年份呢@goofyui欢迎您,在这种情况下,您需要为每个事件添加
year()
函数,例如
sum(当月份(FileReceivedDate)=1和年份(FileReceivedDate)=2019年,然后1个其他0结束)作为2019年1月
sum(当月份(FileReceivedDate)=1和年份(FileReceivedDate)=2018年,然后1个其他0结束)截至2018年1月
。。等