select 姓名 ,sum(case 课程  when '语文' then 分数) end as  语文,   
sum(case 课程  when '数学' then 分数) end as 数学,
sum(case 课程  when '英语' then 分数) end as 英语
group by 姓名

解决方案 »

  1.   

    select 姓名 ,sum(case 课程  when '语文' then 分数 end) as  语文,   
    sum(case 课程  when '数学' then 分数 end) as 数学,
    sum(case 课程  when '英语' then 分数 end) as 英语
    group by 姓名
      

  2.   

    可以创建一个动态的存储过程实现:declare @y varchar(10)
    declare @s varchar(8000)
    set @s=''
    declare a cursor for
      select 课程 from yourtable
    open a
      fetch next from a into @y
      while @@fetch_status=0
       begin
        set @S=@s+',sum(case 课程 when '''+@y+''' then 分数 else 0 end) '''+@y+''''
        fetch next from a into @y
     
       end
     close a
     deallocate aexec('select 姓名'+@s+' from yourtable group by 课程')
      

  3.   

    select 姓名 ,sum(case 课程  when '语文' then 分数 end)  as  语文,   
    sum(case 课程  when '数学' then 分数 end)  as 数学,
    sum(case 课程  when '英语' then 分数 end)  as 英语
    group by 姓名