UNPIVOT in sql server

SELECT id

        ,entityId

        ,indicatorname

        ,indicatorvalue

  FROM (VALUES

        (1, 1, 'Value of Indicator 1 for entity 1', 'Value of Indicator 2 for entity 1', 'Value of Indicator 3 for entity 1'),

        (2, 1, 'Value of Indicator 1 for entity 2', 'Value of Indicator 2 for entity 2', 'Value of Indicator 3 for entity 2'),

        (3, 1, 'Value of Indicator 1 for entity 3', 'Value of Indicator 2 for entity 3', 'Value of Indicator 3 for entity 3'),

        (4, 2, 'Value of Indicator 1 for entity 4', 'Value of Indicator 2 for entity 4', 'Value of Indicator 3 for entity 4')

       ) AS Category(ID, EntityId, Indicator1, Indicator2, Indicator3)

UNPIVOT

(

    indicatorvalue

    FOR indicatorname IN (Indicator1, Indicator2, Indicator3)

) UNPIV;




 

Comments

Popular posts from this blog

unpivot in sql server above version 2016