table1表
年 月 销售额
1999 1 20
1999 2 30
.................
2006 4 50
table2表
年 1月 2月 3月 4月 ........12月
怎么把表1的销售额存入表2啊!
create table tn(年 int,月 int,销售额 int)
insert tn select 1999 ,1,20
union all select 1999 ,2 ,30
union all select 1999 ,3 ,30
union all select 1999 ,4 ,30
union all select 1999 ,5 ,30
union all select 1999 ,6 ,30
union all select 1999 ,7 ,30
union all select 1999 ,8 ,30
union all select 1999 ,9 ,30
union all select 1999 ,10 ,30
union all select 1999 ,11 ,30
union all select 1999 ,12 ,30
union all select 2000 ,2 ,30
union all select 2000 ,3 ,30
go
declare @s varchar(8000)
select @s='select 年'
select @s=@s+',['+cast(月 as varchar)+'月] =sum(case when 月='+cast(月 as varchar)+' then '+cast(月 as varchar)+' end)' from tn group by 月
print @s
exec(@s+' from tn group by 年')
go