BigQuery · Query syntax
BigQuery UNPIVOT
UNPIVOT turns columns into rows: UNPIVOT(amount FOR month IN (jan, feb, mar)) gives one row per month with its value in amount. Rows whose value is NULL are dropped unless you add INCLUDE NULLS.
UNPIVOT is a FROM clause operator that rotates a set of columns into rows. You name a value column (amount) and a name column (month); for each input row, each column in the IN list produces one output row with the column's value and its name. The other input columns are repeated on every output row. By default rows whose value is NULL are left out (EXCLUDE NULLS); add INCLUDE NULLS right after UNPIVOT to keep them, which matters when a missing value should still appear as a row.
The name column holds the column name as a STRING unless you give aliases: IN (q1 AS 'Q1', q2 AS 'Q2'), or INT64 aliases such as IN (q1 AS 1, q2 AS 2), in which case every column needs one. All unpivoted columns must have the same type, without implicit coercion: unpivoting an INT64 column together with a FLOAT64 column fails, so cast them in a subquery first. Expressions and qualified names (t.q1) are not allowed in the IN list.
A multi-column unpivot rotates groups of columns at once: UNPIVOT ((revenue, cost) FOR half IN ((h1_revenue, h1_cost) AS 'H1', (h2_revenue, h2_cost) AS 'H2')). It is the tool for wide exports from spreadsheets and SaaS tools, and the inverse of PIVOT, with one caveat: it cannot undo an aggregation. In PostgreSQL the same result takes CROSS JOIN LATERAL (VALUES ...); in BigQuery you can also write it as CROSS JOIN UNNEST([STRUCT('jan' AS month, jan AS amount), ...]).
Syntax
SELECT ...
FROM from_item
UNPIVOT [{ INCLUDE NULLS | EXCLUDE NULLS }] (
values_column FOR name_column IN (column [[AS] alias][, ...])
) [[AS] alias]
UNPIVOT [{ INCLUDE NULLS | EXCLUDE NULLS }] (
(values_column[, ...]) FOR name_column
IN ((column[, ...]) [[AS] alias][, ...])
)Arguments and return value
| Argument | Description |
|---|---|
values_column | New column that receives the values of the unpivoted columns. |
name_column | New column that receives the name of the source column, or its alias. |
column [AS alias] | Columns to rotate, all of the same type. Aliases are STRING or INT64 literals; with INT64 every column needs one. |
INCLUDE NULLS / EXCLUDE NULLS | Whether rows with a NULL value are returned. The default is EXCLUDE NULLS. |
Returns: One row per input row and unpivoted column (or column set), with the remaining input columns repeated.
Examples
Monthly columns to rows
WITH budget AS (
SELECT 'marketing' AS team, 1200 AS jan, 900 AS feb, 1500 AS mar UNION ALL
SELECT 'support', 400, 400, 450
)
SELECT team, month, amount
FROM budget
UNPIVOT (amount FOR month IN (jan, feb, mar))
ORDER BY team, month;| team | month | amount |
|---|---|---|
| marketing | feb | 900 |
| marketing | jan | 1200 |
| marketing | mar | 1500 |
| support | feb | 400 |
| support | jan | 400 |
| support | mar | 450 |
NULL values disappear unless you ask for them
The support team has no February figure. The default drops that row; INCLUDE NULLS keeps it.
WITH budget AS (
SELECT 'support' AS team, 400 AS jan, CAST(NULL AS INT64) AS feb
)
SELECT team, month, amount
FROM budget
UNPIVOT INCLUDE NULLS (amount FOR month IN (jan AS 'January', feb AS 'February'))
ORDER BY month DESC;| team | month | amount |
|---|---|---|
| support | January | 400 |
| support | February | NULL |
Unpivot pairs of columns at once
WITH finance AS (
SELECT 'shop_de' AS unit, 500 AS h1_revenue, 300 AS h1_cost, 650 AS h2_revenue, 320 AS h2_cost
)
SELECT *
FROM finance
UNPIVOT ((revenue, cost) FOR half IN ((h1_revenue, h1_cost) AS 'H1', (h2_revenue, h2_cost) AS 'H2'))
ORDER BY half;| unit | revenue | cost | half |
|---|---|---|---|
| shop_de | 500 | 300 | H1 |
| shop_de | 650 | 320 | H2 |
Common mistakes
- Losing rows with NULL values. EXCLUDE NULLS is the default; add
INCLUDE NULLSwhen a missing value must still appear. - Mixing column types. INT64 and FLOAT64 (or STRING) columns cannot be unpivoted together; cast them to one type in a subquery first.
- Referencing the new columns' order after
SELECT *. The output puts the remaining input columns first, then the value column(s), then the name column; select columns explicitly if order matters. - Putting expressions in the IN list, such as
IN (jan * 100). Only plain column names are allowed.
Cost and performance
- UNPIVOT multiplies rows: 12 monthly columns give 12 rows per input row before any filter. Filter on the name column after unpivoting only when needed, and select only the columns you rotate so the scan stays small.
FAQ
How do I convert columns to rows in BigQuery?
Use FROM t UNPIVOT (value FOR column_name IN (col_a, col_b, col_c)). Each listed column becomes a row with its value and name.
Why does UNPIVOT drop some rows?
Rows whose value is NULL are excluded by default. Write UNPIVOT INCLUDE NULLS (...) to keep them.
Can UNPIVOT reverse a PIVOT?
It restores the shape, one row per column value, but not the original detail rows, because PIVOT aggregated them.
Related
More query syntax
All BigQuery query syntax · All statements and clauses · BigQuery error messages
