关注,是不是可以用SQL语句来实现?

解决方案 »

  1.   

    declare @s varchar(8000)
    set @s=''
    select @s=@s+',['+科目+']=sum(case 科目 when '''+科目+''' then 成绩 else 0 end)'
    from 表 group by 科目
    exec('select 學號'+@s+' from 表 group by 學號')
      

  2.   

    --测试--测试数据
    create table 表(學號 int,科目 varchar(10),成績 int)
    insert 表 select 1,'語文',80
    union all select 1,'數學',75
    union all select 1,'英語',90
    union all select 1,'物理',80
    union all select 1,'歷史',98
    go--查询
    declare @s varchar(8000)
    set @s=''
    select @s=@s+',['+科目+']=sum(case 科目 when '''+科目+''' then 成績 else 0 end)'
    from 表 group by 科目
    exec('select 學號'+@s+' from 表 group by 學號')
    go--删除测试
    drop table 表/*--测试结果學號   歷史   數學   物理   英語   語文 
    ----- ------ ------ ------ ----- -----
    1     98     75     80     90    80
    --*/