Pivot - Unpivot

  • Hi everybody, i have a table in SQl Server 2014 like the following:

    Image1

    And i woul like to abtain an output like this:

    Image2

    any help is appreciated.

    Thank you

    • This topic was modified 1 year, 11 months ago by  Loneliness.
  • Just a suggestion to a newbie that took the time to nicely format their question... if you were to post your question with some readily consumable data, you'd already have an answer.  Welcome aboard and please see the article at the first link in my signature line below for what I'm talking about and why.

     

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • SELECT ca.*
    FROM dbo.your_table_name
    CROSS APPLY (
    SELECT Sector, 2023 AS Period, [2023] AS Value
    UNION ALL
    SELECT Sector, 2024, [2024]
    UNION ALL
    SELECT Sector, 2025, [2025]
    ) AS ca
    ORDER BY Sector, Period

    SQL DBA,SQL Server MVP(07, 08, 09) A socialist is someone who will give you the shirt off *someone else's* back.

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply