日期:2014-05-18 浏览次数:20539 次
select * from ( select CO_CustomerID,convert(varchar(6),CO_Date,112) YM,SUMQty=SUM(CO_Qty)OVER(partition by CO_CustomerID),CO_Qty) t pivot (SUM(CO_Qty) for YM IN([201101],[201102],....[201112])) pvt
------解决方案--------------------
declare @sql varchar(8000) set @sql = 'select CO_CustomerID , ' select @sql = @sql + ' , sum(case convert(varchar(6),CO_Date,112) when ''' + convert(varchar(6),CO_Date,112) + ''' then CO_Qty else 0 end) [' + convert(varchar(6),CO_Date,112) + ']' from (select distinct convert(varchar(6),CO_Date,112) from tb) as a set @sql = @sql + ',sum(co_qty) as 合计 from tb group by CO_CustomerID' exec(@sql)
------解决方案--------------------
+1