sql server 2008 dynamic pivot based on ordinal value

六眼飞鱼酱① 提交于 2019-12-06 12:06:08

If you were to have a table called Ordinals (or whatever you want) that contains numbers sufficient to cover the maximum number of ordinals you might attain, this may work for you:-

declare @ordList varchar(max);
select @ordList = stuff((select ', [' + rtrim(ordinal) + ']' as [text()] 
from ( 
    select distinct convert(varchar,ordinal) ordinal from Ordinals 
) ords for xml path('')),1,1,'')

exec ('select * from ( select ' + @ordList + ' from dataTable) tbl pivot (max(stringValue) FOR [1] in (' + @ordList + ')) PVT' ) end

This is pretty much the same as the AW sample

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!