PIVOT
constructsupportedQueries & DMLRotates 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
| Name | Required | Description |
|---|---|---|
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. | |
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
- Snowflake supports dynamic pivot. A dynamic pivot query uses the ANY keyword or a subquery in the PIVOT subclause instead of specifying the pivot values explicitly.
- When dynamic pivot is used in a view definition, queries on the view might fail if the underlying data changes so that the pivot output columns are changed.
- Dynamic pivot isn’t supported in the body of a stored procedure or user-defined function (UDF).
- A pivot query that doesn’t use dynamic pivot can return output with duplicate columns. We recommend avoiding output with duplicate columns. A dynamic pivot query deduplicates duplicate columns.
- A pivot query that doesn’t use dynamic pivot might fail if it attempts to CAST a VARIANT column to a different data type. Dynamic pivot queries don’t have this limitation.
- Currently, the PIVOT semantic doesn’t allow multiple aggregations, but you can achieve similar results by using PIVOT with the UNION operator. For an example, see Dynamic pivot with multiple aggregations using UNION.
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.