Request a demo

BigQuery · Query syntax

BigQuery PIVOT

PIVOT turns row values into columns with an aggregate: PIVOT(SUM(revenue) FOR quarter IN ('Q1', 'Q2', 'Q3', 'Q4')) gives one revenue column per quarter. The IN list must be constants, so build it with EXECUTE IMMEDIATE when the values are dynamic.

GoogleSQL for BigQuery · Checked against the official reference in November 2026

PIVOT is an operator in the FROM clause. For each value in the IN list it creates one output column, holding the aggregate of the rows whose FOR column equals that value. Every other column of the input that is not aggregated and not the FOR column becomes an implicit grouping column. That is the part that surprises people: if the input still has an order_id or a timestamp, you get one row per order instead of one per customer. Select only the grouping columns, the pivot column and the measure in a subquery before pivoting.

Column names come from the values: a string becomes the column name as is, a number 1 becomes _1, a date becomes _2026_11_01. Values that are not valid identifiers, or types other than strings, numbers, BOOL, DATE and STRUCT, need an alias: IN ('Q1' AS q1, 'Q2' AS q2). With several aggregates, each needs an alias that is used as a prefix: SUM(units) AS units, COUNT(*) AS orders produces units_Q1, orders_Q1 and so on. A value with no matching rows produces NULL, not 0; wrap the columns in IFNULL or COALESCE in the outer SELECT if you need zeros.

The aggregate must ignore NULL inputs (COUNT(*) is also allowed, and ARRAY_AGG only with IGNORE NULLS), and the IN values must be literals: no subqueries, query parameters or script variables. For a column list that changes, generate the SQL string with STRING_AGG and run it with EXECUTE IMMEDIATE. The reverse operation is UNPIVOT. PIVOT is also written as a SUM(IF(quarter = 'Q1', revenue, NULL)) per column, which is how you would do it in PostgreSQL or MySQL.

Syntax

SELECT ...
FROM from_item
PIVOT (
  aggregate_function(expression) [[AS] prefix][, ...]
  FOR input_column IN (value [[AS] alias][, ...])
) [[AS] table_alias]

Arguments and return value

ArgumentDescription
aggregate_functionAn aggregate that ignores NULL inputs, such as SUM, MAX, MIN, AVG, ANY_VALUE, STRING_AGG or COUNT (COUNT(*) is allowed too); ARRAY_AGG needs IGNORE NULLS. Give each aggregate a prefix alias when there are several.
input_columnThe column whose values become new columns. Must be groupable.
valueA constant to match against input_column. Each value produces one column; an optional alias names it.
from_itemA table or subquery. All its columns other than the aggregated expression and input_column become grouping columns.

Returns: One row per combination of the remaining (grouping) columns, with one column per IN value and aggregate.

Examples

Revenue per region and quarter

WITH sales AS (
  SELECT 'EMEA' AS region, 'Q1' AS quarter, 120 AS revenue UNION ALL
  SELECT 'EMEA', 'Q2', 80 UNION ALL
  SELECT 'EMEA', 'Q2', 40 UNION ALL
  SELECT 'AMER', 'Q1', 200 UNION ALL
  SELECT 'AMER', 'Q3', 90
)
SELECT *
FROM sales
PIVOT (SUM(revenue) FOR quarter IN ('Q1', 'Q2', 'Q3'))
ORDER BY region;
Result
regionQ1Q2Q3
AMER200NULL90
EMEA120120NULL

An extra column silently becomes a grouping column

order_id is still in the input, so PIVOT groups by it and returns one row per order. Selecting only region, quarter and revenue in a subquery fixes it.

WITH sales AS (
  SELECT 'o1' AS order_id, 'EMEA' AS region, 'Q1' AS quarter, 120 AS revenue UNION ALL
  SELECT 'o2', 'EMEA', 'Q2', 80
)
SELECT *
FROM sales
PIVOT (SUM(revenue) FOR quarter IN ('Q1', 'Q2'))
ORDER BY order_id;
Result
order_idregionQ1Q2
o1EMEA120NULL
o2EMEANULL80

Several aggregates with prefixes and column aliases

WITH sales AS (
  SELECT 'EMEA' AS region, 'Q1' AS quarter, 120 AS revenue UNION ALL
  SELECT 'EMEA', 'Q1', 30 UNION ALL
  SELECT 'EMEA', 'Q2', 80
)
SELECT *
FROM sales
PIVOT (
  SUM(revenue) AS revenue,
  COUNT(*) AS orders
  FOR quarter IN ('Q1' AS q1, 'Q2' AS q2)
);
Result
regionrevenue_q1orders_q1revenue_q2orders_q2
EMEA1502801

Dynamic pivot columns with EXECUTE IMMEDIATE

Builds the IN list from the data, then runs the generated query. The lookup query is billed like any other.

DECLARE quarters STRING;
SET quarters = (
  SELECT STRING_AGG(DISTINCT FORMAT("'%s'", quarter), ', ' ORDER BY FORMAT("'%s'", quarter))
  FROM `project.dataset.sales`
);
EXECUTE IMMEDIATE FORMAT("""
  SELECT * FROM (SELECT region, quarter, revenue FROM `project.dataset.sales`)
  PIVOT (SUM(revenue) FOR quarter IN (%s))
""", quarters);

Common mistakes

  • Leaving extra columns in the input. Every column that is not aggregated or pivoted becomes a grouping column, so rows don't collapse. Pivot a subquery that selects only what you need.
  • Expecting 0 for missing combinations. Values with no rows give NULL; use IFNULL(Q1, 0) in the outer query.
  • Passing the IN list as a subquery or variable. PIVOT needs constants; generate the statement with EXECUTE IMMEDIATE instead.
  • Pivoting with ARRAY_AGG(x). Aggregates that keep NULLs are rejected with "Use of aggregate function ARRAY_AGG as PIVOT expression is not supported unless IGNORE NULLS is specified"; write ARRAY_AGG(x IGNORE NULLS).

Cost and performance

  • PIVOT is an aggregation, so cost is the scan of its input: filter and project columns in the subquery before the PIVOT, and only those columns are read.

FAQ

How do I pivot rows to columns in BigQuery?

SELECT * FROM (SELECT key, category, value FROM t) PIVOT (SUM(value) FOR category IN ('a', 'b', 'c')). Each IN value becomes a column.

How do I pivot with a dynamic list of columns?

BigQuery requires constants in the IN list. Build the list with STRING_AGG into a variable and run the query with EXECUTE IMMEDIATE FORMAT(...), as in the last example.

Why are my pivoted values NULL instead of 0?

No input rows matched that value for the group, and aggregates over no rows return NULL. Replace them with IFNULL(column, 0) in the outer SELECT.

Errors you may hit

More query syntax

All BigQuery query syntax · All statements and clauses · BigQuery error messages

Observability for every BigQuery table, in about 15 minutes

Masthead detects freshness, volume and schema anomalies, monitors pipeline errors and maps column-level lineage across all your projects, from logs and metadata only.