SQL Server 2008 UNPIVOT Table

In this article, I describe how to unpivot table.
  • 1966

UNPIVOT Table

In my previous article I describe about PIVOT Table. You can visit from here: SQL Server 2008 PIVIOT Table

UNPIVOT Table is reverse of PIVOT Table.

Example:

Following example explains how to unpivot table.

-- Unpivot Table ordered by CUST

SELECT CustomerName, ProductName, QuantityNeeded

FROM

(

SELECT ProductName,John,Mike

FROM (

SELECT CustomerName, ProductName, QuantityNeeded

FROM ProductSales) up

PIVOT (SUM(QuantityNeeded) FOR CustomerName IN (John,Mike)) AS [pivot]) p

UNPIVOT

(QuantityNeeded FOR  CustomerName IN (John,Mike)

) AS [unpivot]

GO

Output:

unpivotTable-sql.jpg

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.