问题描述如下:
有一个表,存着本季度所有数据,然后我要实现的是根据本季度周汇总,然后用一张表格显示出来,每周一列
year quarter week amount
2012 4 39 100
2012 4 40 100.
.
.
.
.
2012 4 52 200
显示结果:
year Quarter Week1 Week2 ... Week12
2012 4 100 100 ... 200
求指教~~~
------最佳解决方案--------------------
DECLARE @s NVARCHAR(4000)
SET @s = ''
SELECT @s = @s + ',' + QUOTENAME([week]) + '=max(case when [week]='
+ QUOTENAME([week], '''') + ' then [amount] else 0 end)'
FROM tb
GROUP BY [year],[quarter]
EXEC('select [year],[quarter]'+@s+' from tb group by [year],[quarter]')