sqlstreak

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

monthmobiledesktoptv
2026-011648228
2026-0237420754
2026-03608307100
2026-0430313743

Try it: PostgreSQL playground · SQLite playground

How it works

  1. 1
    GROUP BY 1

    One output row per month.

  2. 2
    CASE WHEN device = 'mobile' THEN 1 ELSE 0 END

    1 for a mobile play, 0 for anything else.

  3. 3
    SUM(…) AS mobile

    Adds 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

monthmobiledesktoptv
2026-011648228
2026-0237420754
2026-03608307100
2026-0430313743

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

countrymobile_mindesktop_mintv_min
BR1087262541074
DE685432991124
GB1040244661691
IN1113863921805
US29038143033654

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

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.

Start free