首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

关于行转列的有关问题

2013-01-05 
关于行转列的问题selectstudents.*,语文max(case when Course语文 then Score else 0 end),数学max(c

关于行转列的问题
select 
    students.*,
语文=max(case when Course='语文' then Score else 0 end),
    数学=max(case when Course='数学' then Score else 0 end),
    外语=max(case when Course='外语' then Score else 0 end),
     总成绩=sum(Score)
from 
   students,object where students.stuid=object.stuid
group by students.stuid,students.stuname,students.age,students.sex

怎么转换成动态语句
[解决办法]

declare @s nvarchar(4000)
set @s=''
Select     @s=@s+','+quotename([Course])+'=sum(case when [Course]='+quotename([Course],'''')+' then [Score] else 0 end)'
from Class group by[Course]
exec('select [Student]'+@s+' from Class group by [Student]')

热点排行
Bad Request.