select * from table1 --行转列前格式
select * from table2 --行专列后格式
drop table table2
declare @sql varchar(max)
set @sql='select stype,newcate,cate'
select @sql=@sql+',max(case month when '''+month+''' then cast(sales as numeric(20,2)) else 0 end)['+month+'_sales]' from(select distinct month from table1 )a
set @sql=@sql+' into table2 from table1 group by stype,newcate,cate order by stype,newcate,cate '
--print(@sql) --打印代码进行检查
exec(@sql)