UNPIVOT
constructsupportedQueries & DMLRotates a table by transforming columns into rows. UNPIVOT is a relational operator that accepts two columns (from a table or subquery), along with a list of columns, and generates a row for each column specified in the list. In a query, it is specified in the FROM clause after the table name or subquery. UNPIVOT is not exactly the reverse of PIVOT because it can’t undo aggregations made by PIVOT. This operator can be used to transform a wide table (e.g. empid, jan_sales, feb_sales, mar_sales) into a narrower table (e.g. empid, month, sales).
Syntax
supportedpartialnot supportednot scored
SELECT ...
FROM ...
UNPIVOT [ { INCLUDE | EXCLUDE } NULLS ]
( <value_column>
FOR <name_column> IN (
<col> [ [ AS ] <col_alias> ],
...
)
)
[ ... ]
Parameters
| Name | Required | Description |
|---|---|---|
{ INCLUDE | EXCLUDE } NULLS | Specifies whether to include or exclude rows with NULLs in the name_column: INCLUDE NULLS includes rows with NULLs. EXCLUDE NULLS excludes rows with NULLs. Default: EXCLUDE NULLS | |
value_column | The name to assign to the generated column that will be populated with the values from the columns in the column list. | |
name_column | The name to assign to the generated column that will be populated with the names of the columns in the column list. | |
column_list | The names of the columns in the source table or subquery that will be rotated into a single pivot column. The column names will populate name_column, and the column values will populate value_column. The column_list can only contain literal column names, not a subquery. The columns in column_list must have exactly the same data type, with the following exceptions: The data types for text strings can be different lengths. If the columns contain text strings, different columns can use different data types for text. For example, the list can include a VARCHAR column and a CHAR column. | |
[ AS ] col_alias | Specifies the column alias to use in the result of the UNPIVOT operation instead of the original column names. You can’t use different aliases for the same column name. However, you can’t use the same alias for multiple column names. The AS keyword is optional. |
Usage notes
- UNPIVOT applies to a single table or subquery, so you must specify it immediately after that table or subquery in the FROM clause. If you specify UNPIVOT after a JOIN, the query returns a syntax error ... unexpected 'UNPIVOT' error. To combine UNPIVOT with a join, use one of the following approaches: Specify UNPIVOT immediately after the table that you want to unpivot, and then join the result to other tables.
- Perform the join in a subquery, and then specify UNPIVOT after the subquery.
- For examples, see Use UNPIVOT with joins.
- You can’t use a LATERAL join to directly reference the result set of an UNPIVOT operation. Attempting to do so returns an error. As a workaround, materialize the UNPIVOT result into a temporary table first, then reference that table in the LATERAL join. To create and load the monthly_sales table that is selected from in this example, see the examples section. The following query doesn’t work because LATERAL can’t reference an UNPIVOT result set directly: Copy code SELECT * FROM monthly_sales UNPIVOT (sales FOR month IN (jan, feb, mar, apr)) unpvt JOIN LATERAL (SELECT unpvt.sales AS sales_value) jl; The following CREATE TEMPORARY TABLE statement creates a temporary table to materialize the UNPIVOT result. The query that follows that statement references the temporary table in the LATERAL join: Copy code CREATE OR REPLACE TEMPORARY TABLE unpivot_result AS SELECT * FROM monthly_sales UNPIVOT (sales FOR month IN (jan, feb, mar, apr)); SELECT * FROM unpivot_result JOIN LATERAL (SELECT unpivot_result.sales AS sales_value) jl;
Parameters
{ INCLUDE | EXCLUDE } NULLS: Specifies whether to include or exclude rows with NULLs in the name_column: INCLUDE NULLS includes rows with NULLs. EXCLUDE NULLS excludes rows with NULLs. Default: EXCLUDE NULLS value_column: The name to assign to the generated column that will be populated with the values from the columns in the column list. name_column: The name to assign to the generated column that will be populated with the names of the columns in the column list. column_list: The names of the columns in the source table or subquery that will be rotated into a single pivot column. The column names will populate name_column, and the column values will populate value_column. The column_list can only contain literal column names, not a subquery. The columns in column_list must have exactly the same data type, with the following exceptions: The data types for text strings can be different lengths. If the columns contain text strings, different columns can use different data types for text. For example, the list can include a VARCHAR column and a CHAR column. [ AS ] col_alias: Specifies the column alias to use in the result of the UNPIVOT operation instead of the original column names. You can’t use different aliases for the same column name. However, you can’t use the same alias for multiple column names. The AS keyword is optional.