← Back to PostgreSQL, page 3

postgresql-0047 · Medium mid

Pivot rows into columns with crosstab from the tablefunc extension, and compare it with FILTER.

crosstab(source_sql, category_sql) turns rows of (row key, category, value) into one row per key with a column per category; the output columns must be declared in the query. The two-argument form places values correctly even when some categories are missing. For a few fixed categories, aggregates with FILTER are simpler and need no extension.

Full answer

Tables and rows used here:

sql
CREATE EXTENSION IF NOT EXISTS tablefunc;
CREATE TABLE monthly (store text, month text, revenue int);
INSERT INTO monthly VALUES ('A', 'Oct', 100), ('A', 'Nov', 120), ('A', 'Dec', 90), ('B', 'Oct', 80), ('B', 'Dec', 110);
sql
SELECT * FROM crosstab($$SELECT store, month, revenue FROM monthly ORDER BY 1$$, $$VALUES ('Oct'), ('Nov'), ('Dec')$$) AS ct(store text, oct int, nov int, dec int);

Result on PostgreSQL 17:

text
 store | oct | nov  | dec
-------+-----+------+-----
 A     | 100 | 120  | 90
 B     | 80  | NULL | 110
(2 rows)
sql
SELECT store, sum(revenue) FILTER (WHERE month = 'Oct') AS oct, sum(revenue) FILTER (WHERE month = 'Nov') AS nov, sum(revenue) FILTER (WHERE month = 'Dec') AS dec FROM monthly GROUP BY store ORDER BY store;

Result:

text
 store | oct | nov  | dec
-------+-----+------+-----
 A     | 100 | 120  | 90
 B     | 80  | NULL | 110
(2 rows)

Store B has no November row, and the two-argument crosstab still puts its December value in the dec column, leaving nov NULL; the one-argument form would shift values left into the wrong columns. The FILTER version gives the same table with plain SQL and is easier to maintain when the categories are fixed. Neither approach creates columns dynamically: a truly dynamic pivot needs generated SQL or should be done in the reporting layer. The extension is created inside this item's schema and removed with it.

sql
CREATE EXTENSION IF NOT EXISTS tablefunc;
CREATE TABLE monthly (store text, month text, revenue int);
INSERT INTO monthly VALUES ('A', 'Oct', 100), ('A', 'Nov', 120), ('A', 'Dec', 90), ('B', 'Oct', 80), ('B', 'Dec', 110);

SELECT * FROM crosstab($$SELECT store, month, revenue FROM monthly ORDER BY 1$$, $$VALUES ('Oct'), ('Nov'), ('Dec')$$) AS ct(store text, oct int, nov int, dec int);

SELECT store, sum(revenue) FILTER (WHERE month = 'Oct') AS oct, sum(revenue) FILTER (WHERE month = 'Nov') AS nov, sum(revenue) FILTER (WHERE month = 'Dec') AS dec FROM monthly GROUP BY store ORDER BY store;

Run on PostgreSQL 17.12 (Debian 17.12-0+deb13u1) in a fresh schema, twice, with identical output.

Key points

  • crosstab pivots rows into columns
  • Use the two-argument form for missing categories
  • Output columns must be declared
  • FILTER aggregates pivot fixed categories simply
  • Dynamic pivots need generated SQL

Common mistakes

  • Using one-argument crosstab with gaps
  • Expecting SQL to create columns dynamically

Follow-ups

  • How would you pivot by week for a year?
  • Where should pivoting happen in a BI stack?

Tips

  • Explain the column shift