Skip to main content
The UNPIVOT clause can be used to unpivot columns, “rotating” row values into column values, creating longer, more normalized tables from shorter, more denormalized tables.
UNPIVOT Syntax
For example, given a source table customer_contact, which lists the home, work, & cell phone numbers for each customer in the table, an unpivot operation can be performed, creating separate home, work, & cell phone records for each customer. With this data:
Input
The following unpivot operation can be applied:
UNPIVOT Example
The data will be unpivoted into a table like this:
Output
If the original column names can be used as the values of the unpivot key, as is, the pre-selection and renaming of those columns using a subquery in the FROM clause can be eliminated.For example, unpivoting without aliasing the quarterly grade columns will result in those exact column names being used as the quarter values:
UNPIVOT without Aliases Example
without Aliases Output