How to Pivot Rows to Columns in Snowflake
Last updated July 25, 2026 · By the SaturnSQL team
Use the PIVOT clause with an aggregate and the list of values that become columns, e.g. PIVOT(SUM(amount) FOR status IN ('pending', 'shipped')). For unknown value lists, PIVOT also accepts ANY ORDER BY to build the columns dynamically.
Static pivot
SELECT *
FROM (
SELECT region, status, amount FROM orders
)
PIVOT (
SUM(amount) FOR status IN ('pending', 'shipped', 'cancelled')
) AS p (region, pending, shipped, cancelled);Dynamic pivot
When you cannot enumerate the values up front, IN (ANY ORDER BY ...) creates one column per distinct value at execution time. Column names are then only known at runtime.
SELECT *
FROM (
SELECT region, status, amount FROM orders
)
PIVOT (SUM(amount) FOR status IN (ANY ORDER BY status));The subquery should contain only the grouping column, the pivot column, and the value column; extra columns each become additional group-by keys and multiply the output rows.
Run this in SaturnSQL
SaturnSQL is a browser-based SQL editor for teams: shared query library, schema-aware autocomplete, and scheduled exports to Google Sheets and Slack.
Try it free