动态行转列 pivot实现
declare @sql varchar(8000)
begin
set @sql='' --初始化变量@sql
select @sql=@sql+',['+ convert(varchar(10),CreateDate,120)+']' from vwStationYield
where CreateDate > DATEADD(dd,-14,convert(varchar(10),getdate(),120)) group by convert(varchar(10),CreateDate,120)--变量多值赋值
set @sql=stuff(@sql,1,1,'')--去掉首个','
print @sql
set @sql='select * from (select Product ,Yield, CreateDate from vwStationYield)as st
pivot --行转列
(max(Yield) for CreateDate in('+@sql+'))as p'
exec(@sql)
end