SQL how-to
How to pivot rows into columns in SQL
Updated · Tested on PostgreSQL 18.6 and SQLite 3.54.1 (results generated , re-checked on every release)
Short answer
Group by the row label and write one aggregate per column with a condition: SUM(CASE WHEN device = 'mobile' THEN 1 ELSE 0 END) AS mobile, or the shorter COUNT(*) FILTER (WHERE device = 'mobile'). Both work in PostgreSQL and SQLite.
The task: Turn values from rows into columns, for example one column per device with a count in each.
Plays per month, one column per device
SELECT SUBSTR(CAST(played_on AS TEXT), 1, 7) AS month,
SUM(CASE WHEN device = 'mobile' THEN 1 ELSE 0 END) AS mobile,
SUM(CASE WHEN device = 'desktop' THEN 1 ELSE 0 END) AS desktop,
SUM(CASE WHEN device = 'tv' THEN 1 ELSE 0 END) AS tv
FROM plays
GROUP BY 1
ORDER BY 1;Result (PostgreSQL and SQLite, identical): 4 rows
| month | mobile | desktop | tv |
|---|---|---|---|
| 2026-01 | 164 | 82 | 28 |
| 2026-02 | 374 | 207 | 54 |
| 2026-03 | 608 | 307 | 100 |
| 2026-04 | 303 | 137 | 43 |
Try it: PostgreSQL playground · SQLite playground
How it works
- 1
GROUP BY 1One output row per month.
- 2
CASE WHEN device = 'mobile' THEN 1 ELSE 0 END1 for a mobile play, 0 for anything else.
- 3
SUM(…) AS mobileAdds the 1s: mobile plays in that month, in their own column.
More examples
The same with FILTER
FILTER (WHERE …) attaches the condition to the aggregate. Same result, easier to read.
SELECT SUBSTR(CAST(played_on AS TEXT), 1, 7) AS month,
COUNT(*) FILTER (WHERE device = 'mobile') AS mobile,
COUNT(*) FILTER (WHERE device = 'desktop') AS desktop,
COUNT(*) FILTER (WHERE device = 'tv') AS tv
FROM plays
GROUP BY 1
ORDER BY 1;Result (PostgreSQL and SQLite, identical): 4 rows
| month | mobile | desktop | tv |
|---|---|---|---|
| 2026-01 | 164 | 82 | 28 |
| 2026-02 | 374 | 207 | 54 |
| 2026-03 | 608 | 307 | 100 |
| 2026-04 | 303 | 137 | 43 |
Try it: PostgreSQL playground · SQLite playground
Pivoting sums, not counts
Minutes listened per device instead of plays: put the value in the THEN branch.
SELECT country,
SUM(CASE WHEN device = 'mobile' THEN minutes ELSE 0 END) AS mobile_min,
SUM(CASE WHEN device = 'desktop' THEN minutes ELSE 0 END) AS desktop_min,
SUM(CASE WHEN device = 'tv' THEN minutes ELSE 0 END) AS tv_min
FROM plays p
JOIN users u ON u.id = p.user_id
GROUP BY country
ORDER BY country;Result (PostgreSQL and SQLite, identical): 5 rows
| country | mobile_min | desktop_min | tv_min |
|---|---|---|---|
| BR | 10872 | 6254 | 1074 |
| DE | 6854 | 3299 | 1124 |
| GB | 10402 | 4466 | 1691 |
| IN | 11138 | 6392 | 1805 |
| US | 29038 | 14303 | 3654 |
Try it: PostgreSQL playground · SQLite playground
Variations
Columns you don't know in advance
SQL needs the column list when the query is written. For a changing set of values, generate the query in code, or keep the data in rows and pivot in your spreadsheet or BI tool.
Unpivot: columns back into rows
Stack one SELECT per column with UNION ALL: SELECT month, 'mobile' AS device, mobile AS plays FROM t UNION ALL SELECT month, 'tv', tv FROM t.
Common mistakes
Leaving out
ELSE 0: the CASE gives NULL, and SUM of only NULLs is NULL, not 0.Misspelling a value (
'Mobile'): the column silently stays at 0.Forgetting GROUP BY: you get one row for the whole table.
PostgreSQL and SQLite
Both run CASE WHEN and FILTER (SQLite since 3.30). PostgreSQL's crosstab() needs the tablefunc extension, which isn't loaded here, so the portable forms above are the ones to learn.
Official documentation
Go deeper
- Guide: SQL CASE WHEN
- Lesson: Funnels and conversion (Pro)
Make it stick in five minutes a day
“Funnels and conversion” teaches this step by step on a real database, with instant feedback. Part of sqlstreak Pro; the first lessons are free.