Forum Discussion

benjamin_2024's avatar
benjamin_2024
Copper Contributor
Mar 01, 2025

How do I unpivot so that the value column comes at the end?

hello all, 

 

I have learning the unpivot function, according to the documentation, 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.

  • olafhelper's avatar
    olafhelper
    Bronze 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

     

Resources