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
凑出以上语句,运行就可以。
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
凑出以上语句,运行就可以。
==========
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
close cursorCar
deAllocate cursorCar
改为
close cursorMM
deAllocate cursorMM