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;| team | quarter | target |
|---|---|---|
| north | q1 | 10 |
| north | q2 | 12 |
| north | q3 | 12 |
| north | q4 | 15 |
| south | q1 | 8 |
| south | q2 | 9 |
| south | q3 | NULL |
| south | q4 | 11 |
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;| team | quarter | target |
|---|---|---|
| north | q1 | 10 |
| north | q2 | 12 |
| north | q3 | 12 |
| north | q4 | 15 |
| south | q1 | 8 |
| south | q2 | 9 |
| south | q3 | NULL |
| south | q4 | 11 |
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;| team | quarter | target |
|---|---|---|
| north | q1 | 10 |
| north | q2 | 12 |
| north | q3 | 12 |
| north | q4 | 15 |
| south | q1 | 8 |
| south | q2 | 9 |
| south | q3 | NULL |
| south | q4 | 11 |
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