← Back to SQL, page 7

sql-0136 · Medium mid

How do you unpivot a table with one column per quarter into one row per quarter?

Use UNION ALL with one SELECT per column, each returning the key, a label and that column's value. Alternatively, cross join with a small list of labels and pick the value with CASE. PostgreSQL can also unpivot with a VALUES list in a LATERAL join.

Full answer
sql
CREATE TABLE targets (team TEXT PRIMARY KEY, q1 INTEGER, q2 INTEGER, q3 INTEGER, q4 INTEGER);
INSERT INTO targets VALUES ('north',10,12,12,15),('south',8,9,NULL,11);
sql
SELECT team, 'q1' AS quarter, q1 AS target FROM targets
UNION ALL SELECT team, 'q2', q2 FROM targets
UNION ALL SELECT team, 'q3', q3 FROM targets
UNION ALL SELECT team, 'q4', q4 FROM targets
ORDER BY team, quarter;
teamquartertarget
northq110
northq212
northq312
northq415
southq18
southq29
southq3NULL
southq411

Each branch reads the table once. With four columns that is four scans, which is fine for small tables. The CROSS JOIN version scans once and multiplies rows by the label list:

sql
WITH q(quarter) AS (VALUES ('q1'),('q2'),('q3'),('q4'))
SELECT t.team, q.quarter,
       CASE q.quarter WHEN 'q1' THEN t.q1 WHEN 'q2' THEN t.q2
                      WHEN 'q3' THEN t.q3 ELSE t.q4 END AS target
FROM targets t CROSS JOIN q
ORDER BY t.team, q.quarter;
teamquartertarget
northq110
northq212
northq312
northq415
southq18
southq29
southq3NULL
southq411

PostgreSQL's LATERAL VALUES form is compact:

sql
SELECT t.team, v.quarter, v.target
FROM targets t
CROSS JOIN LATERAL (VALUES ('q1', t.q1), ('q2', t.q2), ('q3', t.q3), ('q4', t.q4)) AS v(quarter, target)
ORDER BY t.team, v.quarter;
teamquartertarget
northq110
northq212
northq312
northq415
southq18
southq29
southq3NULL
southq411

All three return the same eight rows. Wide tables with repeating columns (q1, q2, q3...) often signal a design that should store one row per period; unpivoting is how you read such data in a normalized shape.

sql
CREATE TABLE targets (team TEXT PRIMARY KEY, q1 INTEGER, q2 INTEGER, q3 INTEGER, q4 INTEGER);
INSERT INTO targets VALUES ('north',10,12,12,15),('south',8,9,NULL,11);

SELECT team, 'q1' AS quarter, q1 AS target FROM targets
UNION ALL SELECT team, 'q2', q2 FROM targets
UNION ALL SELECT team, 'q3', q3 FROM targets
UNION ALL SELECT team, 'q4', q4 FROM targets
ORDER BY team, quarter;

WITH q(quarter) AS (VALUES ('q1'),('q2'),('q3'),('q4'))
SELECT t.team, q.quarter,
       CASE q.quarter WHEN 'q1' THEN t.q1 WHEN 'q2' THEN t.q2
                      WHEN 'q3' THEN t.q3 ELSE t.q4 END AS target
FROM targets t CROSS JOIN q
ORDER BY t.team, q.quarter;

Checked on SQLite 3.46.1, run twice with identical output. PostgreSQL results in the answer were checked on PostgreSQL 17.12.

Key points

  • UNION ALL with one branch per column unpivots
  • CROSS JOIN with a label list and CASE scans once
  • PostgreSQL can use LATERAL VALUES
  • Repeating columns usually mean the table should be long, not wide

Common mistakes

  • Using UNION instead of UNION ALL and losing equal values
  • Forgetting the label column that says which column a value came from

Follow-ups

  • How would you skip NULL quarters?
  • How would you pivot the result back?

Tips

  • Mention the normalization angle