Stop 14 · SQL for Data AnalysisTopics · 4/9

Pivoting Rows to Columns

Part of the SQL Roadmap

In short

Managers love a grid: cities down the side, months across the top. Your data is stored one row per city per month. Pivoting turns that long list into the wide table everyone wants to read.

Pivoting turns row values into columns, such as one column per month. The portable way is conditional aggregation, SUM(CASE WHEN month = 'Jan' THEN amount END), since only some databases have a PIVOT keyword (SQL Server and Oracle) or crosstab (PostgreSQL's tablefunc extension).

At a wooden school desk, the jaadu takes small paper slips from a tall pile and places each one into the right square of a large grid-ruled register.

Think of it like this

From attendance slips to the register

Every day, a class monitor writes slips like 'Aditi, Monday, present'. At month end, the teacher copies them into the register: one line per student, one column per day. Same facts, different shape. Pivoting in SQL is that copying job, done with SUM and CASE.

Watch it happen

Long rows become one wide row

Pune | Jan | 120
0
Pune | Feb | 90
1
Pune | Mar | 150
2
Surat | Jan | 70
3
Surat | Mar | 40
4

1/4Long format: one row per city per month. Amounts are in thousands of rupees.

The big ideas

What you need to know

One CASE per column

Each output column is SUM(CASE WHEN category = X THEN value ELSE 0 END). You list the columns yourself.

GROUP BY the row label

Whatever goes down the side, like city, goes in GROUP BY.

FILTER is a shortcut

PostgreSQL's SUM(amount) FILTER (WHERE ...) does the same as SUM(CASE ...), and reads more cleanly.

PIVOT is not portable

SQL Server and Oracle have a PIVOT keyword, PostgreSQL has crosstab in an extension. Conditional aggregation works everywhere.

See it in code

Walk through the code

sql
1SELECT2  city,3  SUM(CASE WHEN EXTRACT(MONTH FROM order_date) = 1 THEN amount ELSE 0 END) AS jan,4  SUM(CASE WHEN EXTRACT(MONTH FROM order_date) = 2 THEN amount ELSE 0 END) AS feb,5  SUM(CASE WHEN EXTRACT(MONTH FROM order_date) = 3 THEN amount ELSE 0 END) AS mar,6  SUM(amount) AS q1_total7FROM orders8WHERE order_date >= DATE '2026-01-01'9  AND order_date <  DATE '2026-04-01'10GROUP BY city11ORDER BY q1_total DESC;12 13-- PostgreSQL shorthand for one pivoted column14SELECT city,15       SUM(amount) FILTER (WHERE EXTRACT(MONTH FROM order_date) = 1) AS jan16FROM orders17GROUP BY city;

1/5City is the row label down the side.

Conditional aggregation works in PostgreSQL, MySQL, SQL Server and most warehouses. FILTER is PostgreSQL and SQLite only, and returns NULL rather than 0 when nothing matches.

Save yourself the pain

Common mistakes

Oops

Using MAX(CASE ...) when a city has several orders per month.

Do this instead

MAX keeps only the biggest order. Use SUM for totals, and MAX only when there's exactly one row per cell.

Oops

Forgetting ELSE 0 and then adding the month columns together.

Do this instead

NULL + 100 is NULL. Use ELSE 0 or COALESCE so totals across columns still work.

Your mission

Build a city by quarter grid

~20 min

Quick check

What's the most portable way to pivot rows into columns across different databases?

Go deeper

  1. 1Build a sales-by-category table with one column per quarter using SUM and CASE.
  2. 2Unpivot it back to rows with UNION ALL.
  3. 3Decide whether pivoting belongs in SQL or in the BI tool for your report.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge