Forum Discussion
benjamin_2024
Mar 01, 2025Copper Contributor
How do I unpivot so that the value column comes at the end?
hello all,
I have learning the unpivot function, according to https://learn.microsoft.com/en-us/sql/t-sql/queries/from-using-pivot-and-unpivot?view=sql-server-ver16, the clause before the FOR keyword comes at the end.
But when I do it, the column before the FOR keyboard comes before the column after the FOR keyboard.
please see this example I did
How can I make the value column to be the final column like the documentation?
Thanks you.
1 Reply
Sort By
- olafhelperBronze Contributor
By avoiding using the asteriks * in queries, additional you can use subqueries or CTE = common table expression.
With subquery it would be
SELECT SUB.PersonIdentifier, SUB.col1, SUB.col, SUB.value FROM (SELECT * FROM ##temp1 UNPIVOT (value for col in (col2, col3, col4)) AS up ) AS SUB