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 6 days, 16 hours 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)
    Intro to Tally Tables and Functions

  • 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) "Money can't buy you happiness." Maybe so, but it can make your unhappiness a LOT more comfortable!

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

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