SQL PIVOT: turning rows into columns
A SQL pivot turns the distinct values of one column into separate columns, with an aggregate filling each cell, such as one row per region and one column per quarter. Some engines have a PIVOT keyword; everywhere else the same result comes from conditional aggregation with CASE or FILTER.
What a pivot does
Data is usually stored long: one row per region, quarter and amount. Reports usually want it wide: one row per region, one column per quarter. A pivot is a GROUP BY on the row key plus one aggregate per output column. UNPIVOT is the reverse: it folds columns back into name and value rows.
The portable way: CASE or FILTER
Conditional aggregation works in every SQL engine. Each output column is an aggregate that only counts rows matching one value. PostgreSQL, SQLite and DuckDB also accept the shorter FILTER clause; MySQL and SQL Server need the CASE form.
CREATE TABLE sales (region VARCHAR, quarter VARCHAR, amount INTEGER);
INSERT INTO sales VALUES
('East', 'Q1', 100), ('East', 'Q2', 150), ('East', 'Q2', 50),
('West', 'Q1', 80), ('West', 'Q3', 120);
-- Any engine
SELECT region,
sum(CASE WHEN quarter = 'Q1' THEN amount END) AS q1,
sum(CASE WHEN quarter = 'Q2' THEN amount END) AS q2,
sum(CASE WHEN quarter = 'Q3' THEN amount END) AS q3
FROM sales
GROUP BY region
ORDER BY region;
-- PostgreSQL, SQLite, DuckDB
SELECT region,
sum(amount) FILTER (WHERE quarter = 'Q1') AS q1,
sum(amount) FILTER (WHERE quarter = 'Q2') AS q2,
sum(amount) FILTER (WHERE quarter = 'Q3') AS q3
FROM sales
GROUP BY region
ORDER BY region;Both return East with 100, 200 and NULL, and West with 80, NULL and 120. A quarter with no rows gives NULL, not 0; wrap the aggregate in coalesce() if the report needs zeros.
SQL Server's PIVOT operator
SQL Server (and Oracle) put PIVOT in the FROM clause. The output columns must be listed by hand. When the values are not known in advance, the usual workaround is to build the statement as a string and run it as dynamic SQL.
SELECT region, [Q1], [Q2], [Q3] FROM (SELECT region, quarter, amount FROM sales) AS src PIVOT (SUM(amount) FOR quarter IN ([Q1], [Q2], [Q3])) AS p;
DuckDB's PIVOT and UNPIVOT
DuckDB has a shorter PIVOT statement. Without an IN list it finds the distinct values itself and creates one column for each, so a new quarter in the data becomes a new column without editing the query. UNPIVOT takes a list of columns, or COLUMNS(* EXCLUDE (...)) for all but the key.
-- Columns come from the data: Q1, Q2, Q3
PIVOT sales ON quarter USING sum(amount) GROUP BY region;
-- Fixed column list, standard FROM-clause form
SELECT *
FROM sales
PIVOT (sum(amount) FOR quarter IN ('Q1', 'Q2', 'Q3') GROUP BY region);
-- Back to long form
CREATE TABLE wide AS
PIVOT sales ON quarter USING sum(amount) GROUP BY region;
UNPIVOT wide
ON COLUMNS(* EXCLUDE (region))
INTO NAME quarter VALUE amount;The UNPIVOT returns 4 rows, not 6: the two empty cells are dropped. Dynamic columns are convenient for exploration, but a downstream report or BI tool expects a fixed schema, so production queries usually list the values.
In Thunderduck
Thunderduck runs DuckDB's dialect, and SELECT is always allowed, so a CASE or FILTER pivot works against any catalog: Parquet files in a bucket, an Iceberg table, or a PostgreSQL or MySQL database. On PostgreSQL and MySQL, filters on constants and the GROUP BY are pushed down to the source when the table is alone in its SELECT, so only the aggregated rows cross the wire.
Docs: https://docs.thunderduck.ai/reference/sql/