我正在尝试创建一个显示销售的查询:
Jan Feb Mar ...
2011 1253 1381 1973 ...
2012 3124 3829 1823 ...
沿着顶部显示从1月到12月的月份,左侧列显示年份。该数据是该年/月的整个期间的SUM'd数字。
该表包含带有总计和日期时间字段的销售记录。
我已经看到了使用UNPIVOT的一些选项,但我无法理解它。
由于
答案 0 :(得分: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 )