CREATE proc p_cggl_jinhuo_rop01 (
--采购一级分类费用汇总表(按进货单:不含税金额), 行转列
@lyear char(4) --年度
)
as
declare @sql nvarchar(4000)
--declare @lyear nvarchar(4)
--declare @lmonth nvarchar(2)
--set @lyear='2018'
--set @lmonth='11'
set @sql=N'select LEFT(年月, 4) as 年,一级分类'
select @sql=@sql+N',sum(case right(年月,6) when '''+[年月]+''' then 不含税金额 else 0 end ) as ['+[不含税月]+']'
from v_caigou_jinhuo_all_xpfl a
where left(年月,4)=@lyear and 商品分类=(select top 1 商品分类 from v_caigou_jinhuo_all_xpfl where right(年月,2)=right(年月,2) and left(年月,4)=@lyear )
order by 商品分类
set @sql=@sql+' from v_caigou_jinhuo_all_xpfl where left(年月,4)='+@lyear+' group by 商品分类,一级分类,LEFT(年月, 4)'
--print @sql
exec(@sql)
GO