BigQuery · Aggregate function
BigQuery SUM
SUM adds up the non-NULL values in a group and returns NULL, not 0, when there is nothing to add. With OVER it gives running totals and shares of a total. INT64 sums stay INT64 and fail on overflow; FLOAT64 sums can differ in the last digits between runs.
SUM ignores NULLs and returns the total of the remaining values. If the group is empty, or every value is NULL, the result is NULL, so reports usually wrap it as COALESCE(SUM(amount), 0). The result type follows the input: INT64 stays INT64, NUMERIC stays NUMERIC, BIGNUMERIC stays BIGNUMERIC and FLOAT64 stays FLOAT64. SUM also adds INTERVAL values.
As a window function, SUM gives running totals (SUM(amount) OVER (ORDER BY day)) and shares of a total (amount / SUM(amount) OVER ()). Watch the default frame: with ORDER BY and no frame clause, BigQuery uses RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, so rows that share an ORDER BY value get the same total. Write ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW with a unique tie-breaker when every row needs its own running total.
Two failure modes depend on the type. An INT64 sum that passes 9,223,372,036,854,775,807 fails with Error in SUM aggregation: integer overflow instead of wrapping around; cast to NUMERIC or BIGNUMERIC first. A FLOAT64 sum is non-deterministic: floating-point addition depends on order, and BigQuery adds values in parallel, so the last digits can change between runs. Keep money in NUMERIC.
Syntax
SUM([DISTINCT] expression [WHERE where_expression]) [OVER over_clause]Arguments and return value
| Argument | Description |
|---|---|
expression | INT64, NUMERIC, BIGNUMERIC, FLOAT64 or INTERVAL. NULLs are skipped. |
DISTINCT | Adds each distinct value once. Rarely right for money: two orders of the same amount are still two orders. |
WHERE where_expression | Optional filter inside the call, such as SUM(amount WHERE status = 'paid') (Preview). SUM(IF(status = 'paid', amount, NULL)) is the generally available equivalent. |
over_clause | Makes SUM a window function. PARTITION BY restarts the total for each group; ORDER BY turns it into a running total. |
Returns: INT64 for INT64 input, NUMERIC for NUMERIC, BIGNUMERIC for BIGNUMERIC, FLOAT64 for FLOAT64, INTERVAL for INTERVAL. NULL for an empty group or all-NULL input.
Examples
Revenue per customer, including customers without orders
c2's only order has no amount and c3 has no orders, so SUM returns NULL for both. COALESCE turns that into 0.
WITH customers AS (
SELECT 'c1' AS customer_id UNION ALL
SELECT 'c2' UNION ALL
SELECT 'c3'
),
orders AS (
SELECT 'c1' AS customer_id, 120 AS amount UNION ALL
SELECT 'c1', 80 UNION ALL
SELECT 'c2', NULL
)
SELECT
customer_id,
SUM(amount) AS revenue,
COALESCE(SUM(amount), 0) AS revenue_or_zero
FROM customers
LEFT JOIN orders USING (customer_id)
GROUP BY customer_id
ORDER BY customer_id;| customer_id | revenue | revenue_or_zero |
|---|---|---|
| c1 | 200 | 200 |
| c2 | NULL | 0 |
| c3 | NULL | 0 |
Running total
WITH daily AS (
SELECT DATE '2026-11-01' AS day, 120 AS revenue UNION ALL
SELECT DATE '2026-11-02', 80 UNION ALL
SELECT DATE '2026-11-03', 200 UNION ALL
SELECT DATE '2026-11-04', 50
)
SELECT
day,
revenue,
SUM(revenue) OVER (ORDER BY day ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS running_total
FROM daily
ORDER BY day;| day | revenue | running_total |
|---|---|---|
| 2026-11-01 | 120 | 120 |
| 2026-11-02 | 80 | 200 |
| 2026-11-03 | 200 | 400 |
| 2026-11-04 | 50 | 450 |
Ties: the default RANGE frame vs ROWS
Two payments share a date. With the default frame, both rows of 2026-11-01 include each other and show 150. The ROWS frame, with amount as a tie-breaker, adds one row at a time.
WITH payments AS (
SELECT DATE '2026-11-01' AS paid_on, 100 AS amount UNION ALL
SELECT DATE '2026-11-01', 50 UNION ALL
SELECT DATE '2026-11-02', 30
)
SELECT
paid_on,
amount,
SUM(amount) OVER (ORDER BY paid_on) AS default_frame,
SUM(amount) OVER (ORDER BY paid_on, amount ROWS UNBOUNDED PRECEDING) AS rows_frame
FROM payments
ORDER BY paid_on, amount;| paid_on | amount | default_frame | rows_frame |
|---|---|---|---|
| 2026-11-01 | 50 | 150 | 50 |
| 2026-11-01 | 100 | 150 | 150 |
| 2026-11-02 | 30 | 180 | 180 |
Share of the total
WITH revenue AS (
SELECT 'EMEA' AS region, 120 AS amount UNION ALL
SELECT 'AMER', 50 UNION ALL
SELECT 'APAC', 30
)
SELECT
region,
amount,
ROUND(amount / SUM(amount) OVER (), 2) AS share_of_total
FROM revenue
ORDER BY amount DESC;| region | amount | share_of_total |
|---|---|---|
| EMEA | 120 | 0.6 |
| AMER | 50 | 0.25 |
| APAC | 30 | 0.15 |
Common mistakes
- Expecting 0 when nothing matches. SUM returns NULL for an empty group or all-NULL input, and NULL spreads through arithmetic (
revenue - refundsbecomes NULL). UseCOALESCE(SUM(x), 0). - Running totals that jump on ties. The default frame is RANGE, so all rows with the same date get that date's full total. Use a ROWS frame and add a tie-breaker column to ORDER BY.
- Summing after a join that multiplies rows. Joining orders to order lines repeats each order's shipping fee once per line, and SUM adds it every time. Aggregate each table to the join key before joining.
- Overflowing INT64. Very large totals fail with
Error in SUM aggregation: integer overflow(see int64 overflow). Cast before summing:SUM(CAST(bytes AS BIGNUMERIC)). - Keeping money in FLOAT64. Float sums can change in the last digits between runs, and 0.1 + 0.2 is not exactly 0.3. Use NUMERIC for amounts.
Cost and performance
- A running total over a whole table (
OVER (ORDER BY ts)without PARTITION BY) sorts every row on a single worker, and on large inputs fails with Resources exceeded. Aggregate to one row per day first, or partition the window by customer or month.
FAQ
How do I calculate a running total in BigQuery?
Use SUM(amount) OVER (ORDER BY day ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW). Add PARTITION BY customer_id to restart the total for each customer.
Why does SUM return NULL instead of 0?
Because there was nothing to add: the group is empty (often after a LEFT JOIN) or every value is NULL. Wrap it in COALESCE: COALESCE(SUM(amount), 0).
How do I calculate a percentage of the total?
Divide each value by a window total: amount / SUM(amount) OVER () for the share of the grand total, or OVER (PARTITION BY region) for the share within each region. Use SAFE_DIVIDE if the total can be 0.
Why does my SUM of FLOAT64 values change between runs?
Floating-point addition depends on order, and BigQuery adds values in parallel, so the order can differ from run to run and the last digits change. Sum NUMERIC values when the result has to be exact.
Related
Errors you may hit
More aggregate functions
All BigQuery aggregate functions · All functions · BigQuery error messages
