0Pricing
SQL Academy · Lesson

Crosstab Patterns (PostgreSQL crosstab())

Generate true pivot tables with the tablefunc extension's crosstab() function.

Why True Crosstab?

CASE pivots require you to list each target column. For genuinely wide pivots (e.g. one column per product), the tablefunc extension's crosstab() is the tool.

Enabling the Extension

tablefunc ships with PostgreSQL contrib:

CREATE EXTENSION IF NOT EXISTS tablefunc;

All lessons in this course

  1. UNION, INTERSECT, EXCEPT
  2. UNION ALL vs UNION (Deduplication Cost)
  3. CASE Expressions and Pivot Queries
  4. Crosstab Patterns (PostgreSQL crosstab())
← Back to SQL Academy