select a.name,sum(case when b.name='语文' then a. else 0 end) as 语文,
 sum(case when b.name='数学' then a. else 0 end) as 数学,
 ...
 sum(a.) as 合计
from table2 a,table1 b
where a.sub_code=b.code     
group by a.name
凑出以上语句,运行就可以。

解决方案 »

  1.   

    但是如果我不知道表1中具体的name的值那该如何办阿??
      

  2.   

    http://expert.csdn.net/Expert/topic/1211/1211043.xml?temp=.3442041
      

  3.   

    我是一个sql server的初学者,我想问一下,在建好临时表后,如何将表1中对应的值插入对应的字段中呢??
      

  4.   

    create table #temp (field1 varchar(10), field2 varchar(20))select field1 ,field2 into #temp from table where ...
                          ==========
      

  5.   

    能,在存储过程中用游标来实现declare @a dec(18,3),@b dec(18,3)
    declare @c dec(18,3),@d dec(18,3)
    declare cursorMM cursor  
    for select a,b,c,d from table1
    open cursorCar
    fetch next from cursorCar into @a,@b,@c,@d
    while @@fetch_status=0
    begin
         insert into table2(a,b,c,d) values(@a,@b,@c,@d)
         fetch next from cursorMM into @a,@b,@c,@d
    end
    close cursorCar
    deAllocate cursorCar
      

  6.   


    close cursorCar
    deAllocate cursorCar
    改为
    close cursorMM
    deAllocate cursorMM