Transpose or unpivot every other column

前端 未结 1 486
慢半拍i
慢半拍i 2021-01-22 09:40

Basically what I am trying to do is transpose every other column to a row with the following columns data beside it.

The source can have only two columns up to one-thou

相关标签:
1条回答
  • 2021-01-22 10:30

    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

    0 讨论(0)
提交回复
热议问题