PIVOT REQUIRED

  • TABLE NAME : UNPIVOT

    IN THE SOURCE UNPIVOT TABLE DATA IS PRESENT LIKE THIS

    COLUMN1 | COLUMN2 | COLUMN3 | COLUMN4

    A | B | P | 1

    A | B | Q | 2

    A | B | R | 3

    I WANT THE FINAL OUTPUT IN THIS WAY

    COLUMN3 VALUES SHOULD BE THE COLUMN NAMES AND THE COLUMN4 DATA IS THE VALUES FROM COLUMN3

    COLUMN1 | COLUMN2 | P | Q | R

    A | B | 1 | 2| 3

    THANKS IN ADVANCE

  • select COLUMN1, COLUMN2,

    sum(case when COLUMN3 =N'P' then COLUMN4 end) as [P],

    sum(case when COLUMN3 =N'Q' then COLUMN4 end) as

    ,

    sum(case when COLUMN3 =N'R' then COLUMN4 end) as [R]

    from #Table

    For more about Cross Tabs and Pivots http://www.sqlservercentral.com/articles/T-SQL/63681/

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

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