I read this SQL Server : Columns to Rows . How can I make sure that the order in which the rows appear in the result will be the same as the order of the columns?
I read this SQL Server : Columns to Rows . How can I make sure that the order in which the rows appear in the result will be the same as the order of the columns?
1条答案
按热度按时间brccelvz1#
As I mentioned in the comments, with an
ORDER BY
. If you're using theUNPIVOT
operator, you may find this difficult if the names of the columns alphabetically don't match the ordinal positions. For example, if you have the following columns in the following orderSurname
,FirstName
,PreferredColour
, then ordering the columns by name would get youFirstName
,PreferredColour
,Surname
.As
UNPIVOT
is pretty restrictive (likePIVOT
), you can instead use aVALUES
table construct. This makes it much easier, as you can add a column to the construct toORDER BY
: