Search code examples
sqlsql-servermultiple-columnsrowsunpivot

Converting multiple Columns of a table into multiple Rows with all other rows repeating


I have something like this

Id ddf1 ddf2 ddf3 ddf4
1 223 112 221 500

I want something like this

Id ddfs
1 223
1 112
1 221
1 500

I've other columns too in my query. So i want a solution which doesn't effect my other columns.


Solution

  • you can do something like this. See working fiddle here

    select id,ddfs
    from yourtable t cross apply
    (values ('ddf1',t.ddf1),('ddf2',t.ddf2), ('ddf3',t.ddf3) ,('ddf4',t.ddf4)) v(k,ddfs)