0

我正在尝试创建一个查询,它将显示这样的销售额:

     Jan  Feb  Mar  ...
2011 1253 1381 1973 ...
2012 3124 3829 1823 ...

顶部显示从 1 月到 12 月的月份,左侧列显示年份。该数据是该年/月整个期间的 SUM 数据。

该表包含总计和日期时间字段的销售记录。

我已经看到了一些使用 UNPIVOT 的选项,但我无法理解它。

谢谢

4

1 回答 1

1

在没有看到您的表结构的情况下,您可以执行类似的操作

select
      year( YT.YourDateColumn ) as SalesYear,
      sum( if( month( yt.YourDateColumn ) = 1, YT.SalesAmt, 000000.00 )) as Jan,
      sum( if( month( yt.YourDateColumn ) = 2, YT.SalesAmt, 000000.00 )) as Feb,
      sum( if( month( yt.YourDateColumn ) = 3, YT.SalesAmt, 000000.00 )) as Mar,
      sum( if( month( yt.YourDateColumn ) = 4, YT.SalesAmt, 000000.00 )) as Apr,
      sum( if( month( yt.YourDateColumn ) = 5, YT.SalesAmt, 000000.00 )) as May,
      sum( if( month( yt.YourDateColumn ) = 6, YT.SalesAmt, 000000.00 )) as Jun,
      sum( if( month( yt.YourDateColumn ) = 7, YT.SalesAmt, 000000.00 )) as Jul,
      sum( if( month( yt.YourDateColumn ) = 8, YT.SalesAmt, 000000.00 )) as Aug,
      sum( if( month( yt.YourDateColumn ) = 9, YT.SalesAmt, 000000.00 )) as Sep,
      sum( if( month( yt.YourDateColumn ) = 10, YT.SalesAmt, 000000.00 )) as Oct,
      sum( if( month( yt.YourDateColumn ) = 11, YT.SalesAmt, 000000.00 )) as Nov,
      sum( if( month( yt.YourDateColumn ) = 12, YT.SalesAmt, 000000.00 )) as Dec
   from
      YourTable YT
   group by
      year( YT.YourDateColumn )
   order by
      Year( YT.YourDateColumn )
于 2012-05-19T00:12:31.237 回答