How to Use PIVOT in BigQuery

Last updated July 26, 2026 · By the SaturnSQL team

PIVOT turns distinct values from one column into separate output columns, aggregating as it goes. It is the built-in alternative to a CASE WHEN per column.

SELECT *
FROM (
  SELECT order_id, status, order_total
  FROM orders
)
PIVOT (
  SUM(order_total) AS total
  FOR status IN ('pending', 'shipped', 'cancelled')
);

The CASE WHEN equivalent

PIVOT is shorthand for a pattern you could also write manually with conditional aggregation; PIVOT just saves the repetition and locks in the column list explicitly.

SELECT order_id,
       SUM(CASE WHEN status = 'pending' THEN order_total END) AS pending,
       SUM(CASE WHEN status = 'shipped' THEN order_total END) AS shipped,
       SUM(CASE WHEN status = 'cancelled' THEN order_total END) AS cancelled
FROM orders
GROUP BY order_id;

PIVOT requires listing the exact values to spread into columns ahead of time; it cannot discover them dynamically at query time.

Run this in SaturnSQL

SaturnSQL is a browser-based SQL editor for teams: shared query library, schema-aware autocomplete, and scheduled exports to Google Sheets and Slack.

Try it free

Related BigQuery guides