UNPIVOT

constructsupportedQueries & DML

docs.snowflake.com ↗

Rotates 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

NameRequiredDescription
{ INCLUDE | EXCLUDE } NULLSSpecifies 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_columnThe name to assign to the generated column that will be populated with the values from the columns in the column list.
name_columnThe name to assign to the generated column that will be populated with the names of the columns in the column list.
column_listThe 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_aliasSpecifies 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

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.

See also

Referenced by