这个,动态行转列的列名能不能按一定顺序排序?
本帖最后由 mjjing 于 2013-01-08 17:55:55 编辑
declare @s nvarchar(4000)set @s=''Select @s=@s+','+quotename([Course])+'=max(case when [Course]='+quotename([Course],'''')+' then [Score] else 0 end)'from Class group by[Course]exec('select [Student]'+@s+' from Class group by [Student]')
DECLARE @s NVARCHAR(4000)
SET @s = ''
SELECT @s = @s + ',' + QUOTENAME([Course]) + '=max(case when [Course]='
+ QUOTENAME([Course], '''') + ' then [Score] else 0 end)'
FROM (SELECT * FROM Class ORDER BY [日期]) Class
GROUP BY [Course]
EXEC('select [Student]'+@s+' from Class group by [Student]')
declare @s nvarchar(4000)
set @s=''
Select @s=@s+','+quotename(CONVERT(varchar(10),日期列,120))+'=max(case when CONVERT(varchar(10),日期列,120)='+quotename([日期列],'''')+' then [Score] else 0 end)'
from Class
group by CONVERT(varchar(10),日期列,120)
order by Min(日期列)