# How to pivot rows into columns in SQL

Source: https://sqlstreak.com/sql/how-to/pivot-rows-to-columns · Tested on PostgreSQL 18.6 and SQLite 3.54.1; results generated 2026-11-22.


**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

```sql
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 (both engines):

| month | mobile | desktop | tv |
| --- | --- | --- | --- |
| 2026-01 | 164 | 82 | 28 |
| 2026-02 | 374 | 207 | 54 |
| 2026-03 | 608 | 307 | 100 |
| 2026-04 | 303 | 137 | 43 |

### The same with FILTER

`FILTER (WHERE …)` attaches the condition to the aggregate. Same result, easier to read.

```sql
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 (both engines):

| month | mobile | desktop | tv |
| --- | --- | --- | --- |
| 2026-01 | 164 | 82 | 28 |
| 2026-02 | 374 | 207 | 54 |
| 2026-03 | 608 | 307 | 100 |
| 2026-04 | 303 | 137 | 43 |

### Pivoting sums, not counts

Minutes listened per device instead of plays: put the value in the THEN branch.

```sql
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 (both engines):

| 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 |

## How it works

- `GROUP BY 1`: One output row per month.
- `CASE WHEN device = 'mobile' THEN 1 ELSE 0 END`: 1 for a mobile play, 0 for anything else.
- `SUM(…) AS mobile`: Adds the 1s: mobile plays in that month, in their own column.

## 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.
