Username Not Exist
Username Not Exist

Reputation: 523

SQL server convert one single row to column

In SQL Server, how can I convert one single row of integer data, like this

ColumnName1    ColumnName2    ColumnName3
    1               2              3 

into a single row, order by DSEC?

ColumnNameTotalSort
        3
        2
        1

I know the requirement seems simple but I have been struggling for a while.

Thanks for input.

Upvotes: 1

Views: 40

Answers (1)

John Cappelletti
John Cappelletti

Reputation: 81930

As ZLK mentioned, UNPIVOT is an option

Another option is with a CROSS APPLY and VALUES

Example

Select B.* 
 From  YourTable A
 Cross Apply (values (ColumnName1)
                    ,(ColumnName2)
                    ,(ColumnName3)
             ) B(ColumnNameTotalSort)
 -- Where Your Filter Condition Here
 Order By ColumnNameTotalSort Desc

Returns

ColumnNameTotalSort
3
2
1

Upvotes: 1

Related Questions