Skip to content
Advertisement

Get the rows of my output sorted in ascending order from this table?

I am using SQL Server 2014 and I have the following table (t1) which contains around 5,000 rows. Here is an extract of t1:

I need select all the rows from the table but I want the output to be sorted in ascending for each row.

Here is the expected output:

I know how to ORDER BY columns but I am completely stuck as to how to approach this problem. Do I need to pivot the data, sort it and then unpivot it to achieve this?

Note: I have added an Id column to my column. Each row now has a unique Id (1,2,3,…)

My attempt as per Menno’s Solution:

Advertisement

Answer

Assuming that there is some kind of ID column, then you can unpivot the values, and the Pivot them back using VALUES and a Cross Tab:

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