Interview Preparation mode beta
Funny Facebook Status Funny Facebook Status
Enter your email address

What is UNPIVOT, in MS SQL Server?

Nice?Vote!
asked 1 year ago in SQL Server Interview Questions and Answers by R (19,530 points) edited 1 year ago by R

1 Answer

Nice?Vote!
The UNPIVOT operator is the opposite of the PIVOT operator.  The PIVOT operator takes a normalized table and transforms it into a new table where the columns of the new table are derived from the values in the original table.  The UNPIVOT operator takes a pivoted table and transforms it back into a normalized form with one row per data point using the column names as values in the result.

Example:
SELECT EId, CAST (Yr AS INT) AS Yr, Sales
FROM (SELECT EmpId*10 AS EId, [2005], [2006], [2007] FROM PIVOT_Sales) AS p
UNPIVOT (Sales FOR Yr IN ([2005], [2006], [2007])) AS s
answered 1 year ago by anonymous

Related questions