Skip to content
Advertisement

How to order columns results in another column (SQL Server)

I have this table

The key columns in my table are RowCnt , Lvl , a_TargetID , b_TargetID

I want to put the name of the column as the order of their value in the a_1st a_2nd a_3rd a_4th

so the final results will be like this

I can do this but it seems very wrong

I wonder if someone has a better method to do that

Advertisement

Answer

You can UNPIVOT and then PIVOT your data within a CROSS APPLY

Results

enter image description here

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement