日期:2014-05-18 浏览次数:20449 次
create table kkk(项目id int,月份 varchar(10),项目值 int) insert into kkk select 1,'1月',1 insert into kkk select 1,'2月',2 insert into kkk select 1,'3月',3 insert into kkk select 1,'4月',4 insert into kkk select 1,'5月',5 insert into kkk select 1,'6月',6 insert into kkk select 1,'7月',7 insert into kkk select 1,'8月',8 insert into kkk select 1,'9月',9 insert into kkk select 1,'10月',10 insert into kkk select 1,'11月',11 insert into kkk select 1,'12月',12 insert into kkk select 2,'1月',21 insert into kkk select 2,'2月',22 insert into kkk select 2,'3月',23 insert into kkk select 2,'4月',24 insert into kkk select 2,'5月',25 insert into kkk select 2,'6月',26 insert into kkk select 2,'7月',27 insert into kkk select 2,'8月',28 insert into kkk select 2,'9月',29 insert into kkk select 2,'10月',30 insert into kkk select 2,'11月',31 insert into kkk select 2,'12月',32 创建存储过程: create proc wsaa as declare @sql varchar(8000) set @sql='select 项目id' select @sql=@sql+',['+月份+']=sum(case 月份 when '''+ 月份 + ''' then 项目值 else 0 end)' from kkk group by 月份 print @sql set @sql=@sql+' from kkk group by 项目id ' exec(@sql) 调用存储过程 exec wsaa