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
本文介绍了一种将一个表格中的数据按月份汇总并迁移到另一个表格的方法。通过使用SQL语句创建临时表,并利用UNION ALL及GROUP BY进行数据汇总,实现了从table1到table2的数据迁移。此过程涉及SQL语句的构造与执行。
2945

被折叠的 条评论
为什么被折叠?



