Search code examples
sqlt-sqlsql-server-2012unpivot

TSQL Un-pivot 2 columns


Good afternoon stackoverflow.

I need help with pivoting some data. I have tried looking up how to do it on the internet but not had any luck.

I have 4 columns (a,b,c,d) and one row of data.

I would like the data to have 2 columns with 4 rows.

a b c d
1 2 3 4

to become

a 1
b 2
c 3
d 4

SQL fiddle here http://sqlfiddle.com/#!6/68b32/280

Thank you for your time


Solution

  • SELECT letter, val
    FROM @t
    unpivot(val for letter in (a,b,c,d)) as unpvt
    

    SQL Fiddle