Transpose or unpivot every other column

妖精的绣舞 提交于 2019-12-02 01:21:35
Cade Roux

If you are using SQL Server 2008, I think you should look into using the CROSS APPLY (VALUES) technique:

http://www.sqlservercentral.com/articles/CROSS+APPLY+VALUES+UNPIVOT/91234/

Basically, it goes like this:

SELECT
  x.NewColumn1,
  x.NewColumn2
FROM YourTable t
CROSS APPLY (
    VALUES
        (t.OldColumn1, t.OldColumn2),
        (t.OldColumn3, t.OldColumn4),
        ...
) x (NewColumn1, NewColumn2);

Here's a runnable example: http://sqlfiddle.com/#!3/9a9d2/5/0

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