数据班级     学生      学科     成绩
A        学生A    数学      80
A        学生B    数学      90
A        学生A    语文      100
A        学生B    语文      80
A        学生C    英语      50
B        学生1    数学      80
B        学生2    数学      90
B        学生1   语文      100
B        学生2    语文      80统计结果(没有总分的项也要显示为0)
班级    语文总分    数学总分    英语总分
A       180          170           50
B        180         170            0
怎么可以用SQL语句形成这样的结果呀,请大家帮忙呀

解决方案 »

  1.   

    create table tb(班级 varchar(10),学生 varchar(10),学科 varchar(10),成绩 int)
    insert into tb select 'A'      ,  '学生A'   , '数学'   ,   80
    union all select 'A'        ,'学生B'   , '数学'      ,90
    union all select 'A'        ,'学生A'   , '语文'      ,100
    union all select 'A'        ,'学生B'   , '语文'     , 80
    union all select 'A'        ,'学生C'   , '英语'     , 50
    union all select 'B'        ,'学生1'   , '数学'     , 80
    union all select 'B'        ,'学生2'   , '数学'     , 90
    union all select 'B'        ,'学生1'   ,'语文'      ,100
    union all select 'B'        ,'学生2'   , '语文'     , 80
    --静态
    select 班级,
           语文总分=sum(case 学科 when '语文' then 成绩 else 0 end),
           数学总分=sum(case 学科 when '数学' then 成绩 else 0 end),
           英语总分=sum(case 学科 when '英语' then 成绩 else 0 end)
    from tb 
    group by 班级--动态
    declare @sql varchar(8000)
    set @sql='select 班级'
    select @sql=@sql+',['+学科+'总分]=sum(case 学科 when '''+学科+''' then 成绩 else 0 end)' from tb group by 学科
    exec(@sql+' from tb group by 班级')
    drop table tb
      

  2.   

    if exists(select * from sysobjects where name='test1' and xtype='U') drop table test1
    create table test1
    (
    班级 varchar(10),
    学生 varchar(10),
    学科 varchar(10),
    成绩 int
    )
    insert into test1
    select 'A','学生A', '数学',80 union all
    select 'A','学生B', '数学',90 union all
    select 'A','学生A', '语文',100 union all
    select 'A','学生B', '语文',80 union all
    select 'A','学生C', '英语',50 union all
    select 'B','学生1', '数学',80 union all
    select 'B','学生2', '数学',90 union all
    select 'B','学生1', '语文',100 union all
    select 'B','学生2', '语文',80
    select
    班级,
    sum(case 学科 when '语文' then 成绩 else 0 end) as 语文总分,
    sum(case 学科 when '数学' then 成绩 else 0 end) as 数学总分,
    sum(case 学科 when '英语' then 成绩 else 0 end) as 英语总分
    from test1
    group by 班级
    drop table test1
      

  3.   

    declare @t table (
    班级 varchar(10),
    学生 varchar(10),
    学科 varchar(10),
    成绩 int)
    insert into @t
    select 'A','学生A', '数学',80 union all
    select 'A','学生B', '数学',90 union all
    select 'A','学生A', '语文',100 union all
    select 'A','学生B', '语文',80 union all
    select 'A','学生C', '英语',50 union all
    select 'B','学生1', '数学',80 union all
    select 'B','学生2', '数学',90 union all
    select 'B','学生1', '语文',100 union all
    select 'B','学生2', '语文',80
    --效率低的方法。
    select 班级 ,
    isnull((select sum (成绩 ) from @t where a.班级 =班级 and 学科='语文'),0) as 语文总分, 
    isnull((select sum (成绩 ) from @t where a.班级 =班级 and 学科='英语'),0) as 语文总分, 
    isnull((select sum (成绩 ) from @t where a.班级 =班级 and 学科='数学'),0) as 语文总分
    from @t a 
    group by 班级
    --效率高
    select 班级 ,
    isnull(sum(case 学科 when  '语文' then 成绩 else 0 end   ),0) as 语文总分, 
    isnull(sum(case 学科 when  '英语' then 成绩 else 0 end  ),0) as 语文总分, 
    isnull(sum(case 学科 when  '数学' then 成绩 else 0 end  ),0) as 语文总分
    from @t 
    group by 班级
    --科目不确定可以参考楼上