PIVOT

constructsupportedQueries & DML

docs.snowflake.com ↗

Rotates a table by turning the unique values from one column in the input expression into multiple columns and aggregating results where required on any remaining column values. In a query, it is specified in the FROM clause after the table name or subquery. PIVOT supports the following built-in aggregate functions: - AVG - COUNT - MAX - MIN - SUM PIVOT can be used to transform a narrow table (for example, empid, month, sales) into a wider table (for example, empid, jan_sales, feb_sales, mar_sales).

Syntax

supportedpartialnot supportednot scored

SELECT ...
FROM ...
   PIVOT ( <aggregate_function> ( <pivot_column> ) [ [ AS ] <alias> ]
            FOR <value_column> IN (
              <pivot_value_1> [ [ AS ] <alias> ] [ , <pivot_value_2> [ [ AS ] <alias> ] ... ]
              | ANY [ ORDER BY ... ]
              | <subquery>
            )
            [ DEFAULT ON NULL (<value>) ]
         )

[ ... ]

Parameters

NameRequiredDescription
aggregate_functionThe aggregate function for combining the grouped values from pivot_column.
pivot_column [ [ AS ] alias ]The column from the source table or subquery that will be aggregated. The optional [ AS ] alias clause specifies the alias to use for the aggregate in the result of the PIVOT operation. An underscore and then the alias is appended to each pivot column name. For example, if the alias is total, then the pivot operation appends _TOTAL to the pivot column names. The AS keyword is optional.
value_columnThe column from the source table or subquery that contains the values from which column names will be generated.
pivot_value_N [ [ AS ] alias ]A list of values for the pivot column to pivot into headings in the query results. The optional [ AS ] alias clause specifies the alias to use for the value in the result of the PIVOT operation. The alias replaces the value.
DEFAULT ON NULL (value)Replace all NULL values in the pivot result with the specified default value. The default value can be any scalar expression that does not depend on the pivot and aggregation column.

Usage notes

Parameters

aggregate_function: The aggregate function for combining the grouped values from pivot_column. pivot_column [ [ AS ] alias ]: The column from the source table or subquery that will be aggregated. The optional [ AS ] alias clause specifies the alias to use for the aggregate in the result of the PIVOT operation. An underscore and then the alias is appended to each pivot column name. For example, if the alias is total, then the pivot operation appends _TOTAL to the pivot column names. The AS keyword is optional. value_column: The column from the source table or subquery that contains the values from which column names will be generated. pivot_value_N [ [ AS ] alias ]: A list of values for the pivot column to pivot into headings in the query results. The optional [ AS ] alias clause specifies the alias to use for the value in the result of the PIVOT operation. The alias replaces the value. ANY [ ORDER BY ... ] Pivot on all distinct values of the pivot column. To control the order of the pivot columns in the output, specify an ORDER BY clause after the ANY keyword. If the pivot column contains NULLs, then NULL is also treated as a pivot value. Pivot on all values found in the subquery. The DISTINCT keyword is required if the subquery includes an ORDER BY clause. The subquery must be an uncorrelated subquery that returns a single column. Pivoting is performed on all distinct values returned by the subquery. For information about uncorrelated subqueries, see Working with Subqueries. DEFAULT ON NULL (value): Replace all NULL values in the pivot result with the specified default value. The default value can be any scalar expression that does not depend on the pivot and aggregation column.

See also

Referenced by