Request a demo

BigQuery · Aggregate function

BigQuery AVG

AVG returns the mean of the non-NULL values in a group. INT64 input returns FLOAT64 and NUMERIC returns NUMERIC. With OVER and a ROWS or RANGE frame it computes moving averages, such as a 7-day average of daily signups.

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

AVG skips NULLs in both the total and the count, so AVG(x) equals SUM(x) / COUNT(x), not SUM(x) / COUNT(*). If a missing value should count as zero, convert it first: AVG(IFNULL(x, 0)). An empty group, or one where every value is NULL, returns NULL.

The result type depends on the input: INT64 and FLOAT64 give FLOAT64, NUMERIC gives NUMERIC, BIGNUMERIC gives BIGNUMERIC and INTERVAL gives INTERVAL. Redshift and SQL Server return an integer for integer input and drop the fraction; BigQuery keeps it, so migrated reports can differ in the decimals. FLOAT64 averages can vary in the last digits between runs; average NUMERIC values when the result must be exact.

With OVER, AVG computes moving averages. ROWS BETWEEN 6 PRECEDING AND CURRENT ROW averages the current row and the six rows before it, which is a 7-day average only if every day has exactly one row. When days can be missing, order by the day number from UNIX_DATE and use a RANGE frame: RANGE frames with offsets need a numeric ORDER BY key, and a DATE is rejected.

Syntax

AVG([DISTINCT] expression [WHERE where_expression]) [OVER over_clause]

Arguments and return value

ArgumentDescription
expressionINT64, NUMERIC, BIGNUMERIC, FLOAT64 or INTERVAL. NULLs are skipped.
DISTINCTAverages each distinct value once.
WHERE where_expressionOptional filter inside the call (Preview), such as AVG(amount WHERE country = 'DE').
over_clauseMakes AVG a window function. A ROWS or RANGE frame turns it into a moving average.

Returns: FLOAT64 for INT64 and FLOAT64 input, NUMERIC for NUMERIC, BIGNUMERIC for BIGNUMERIC, INTERVAL for INTERVAL. NULL for an empty group or all-NULL input.

Examples

NULL is skipped, not counted as zero

c2 has one order without an amount: AVG averages the single known value, while AVG(IFNULL(amount, 0)) counts the missing one as 0. c3 has no amounts at all, so its plain average is NULL.

WITH orders AS (
  SELECT 'c1' AS customer_id, 40 AS amount UNION ALL
  SELECT 'c1', 35 UNION ALL
  SELECT 'c2', 120 UNION ALL
  SELECT 'c2', NULL UNION ALL
  SELECT 'c3', NULL
)
SELECT
  customer_id,
  AVG(amount) AS avg_amount,
  AVG(IFNULL(amount, 0)) AS avg_with_zeros
FROM orders
GROUP BY customer_id
ORDER BY customer_id;
Result
customer_idavg_amountavg_with_zeros
c137.537.5
c2120.060.0
c3NULL0.0

Three-day moving average

WITH daily AS (
  SELECT DATE '2026-11-01' AS day, 100 AS signups UNION ALL
  SELECT DATE '2026-11-02', 130 UNION ALL
  SELECT DATE '2026-11-03', 70 UNION ALL
  SELECT DATE '2026-11-04', 160
)
SELECT
  day,
  signups,
  AVG(signups) OVER (ORDER BY day ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) AS avg_3_rows
FROM daily
ORDER BY day;
Result
daysignupsavg_3_rows
2026-11-01100100.0
2026-11-02130115.0
2026-11-0370100.0
2026-11-04160120.0

Missing days: ROWS vs RANGE

There is no data for November 3 and 4. The ROWS frame still averages the last three rows. The RANGE frame over UNIX_DATE(day) covers the last three calendar days, which on November 5 is that day alone.

WITH daily AS (
  SELECT DATE '2026-11-01' AS day, 100 AS signups UNION ALL
  SELECT DATE '2026-11-02', 130 UNION ALL
  SELECT DATE '2026-11-05', 70
)
SELECT
  day,
  signups,
  AVG(signups) OVER (ORDER BY day ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) AS avg_last_3_rows,
  AVG(signups) OVER (ORDER BY UNIX_DATE(day) RANGE BETWEEN 2 PRECEDING AND CURRENT ROW) AS avg_last_3_days
FROM daily
ORDER BY day;
Result
daysignupsavg_last_3_rowsavg_last_3_days
2026-11-01100100.0100.0
2026-11-02130115.0115.0
2026-11-0570100.070.0

FLOAT64 vs NUMERIC

Averaging the FLOAT64 values 0.1 and 0.2 shows binary rounding error. Casting to NUMERIC gives the exact decimal result.

SELECT
  AVG(price) AS avg_float64,
  AVG(CAST(price AS NUMERIC)) AS avg_numeric
FROM UNNEST([0.1, 0.2]) AS price;
Result
avg_float64avg_numeric
0.150000000000000020.15

Common mistakes

  • Treating NULL as zero. AVG skips NULLs, so the denominator shrinks. If a missing value means 0, use AVG(IFNULL(x, 0)).
  • Averaging averages. The mean of daily averages weighs a day with 3 orders the same as a day with 3,000. Compute SUM(revenue) / SUM(orders), or run AVG over the detail rows.
  • Calling a ROWS frame a 7-day average when days can be missing. Seven rows can span ten days. Use RANGE over UNIX_DATE(day), or fill the missing days first.
  • Mixing AVG with row-level columns. SELECT customer_id, order_id, AVG(amount) ... GROUP BY customer_id fails with SELECT list expression references column which is neither grouped nor aggregated. AVG(amount) OVER (PARTITION BY customer_id) keeps every row and adds the group average.

Cost and performance

  • A moving average over raw events with OVER (ORDER BY event_ts) and no PARTITION BY sorts the whole input on one worker and can fail with Resources exceeded. Aggregate to one row per day (or per entity and day) first, then run the window over those rows.

FAQ

How do I calculate a moving average in BigQuery?

Use AVG(value) OVER (ORDER BY day ROWS BETWEEN 6 PRECEDING AND CURRENT ROW) for the last 7 rows. If days can be missing, order by UNIX_DATE(day) and use RANGE BETWEEN 6 PRECEDING AND CURRENT ROW.

Does AVG ignore NULL values?

Yes. NULLs are left out of both the total and the count. Use AVG(IFNULL(x, 0)) to count them as zeros.

How do I round an average to two decimals?

Use ROUND: ROUND(AVG(x), 2). For an exact decimal result, average NUMERIC values: ROUND(AVG(CAST(x AS NUMERIC)), 2).

How do I get the median instead of the mean?

BigQuery has no MEDIAN function. In a GROUP BY query use APPROX_QUANTILES(x, 2)[OFFSET(1)]; for an exact value use the window function PERCENTILE_CONT(x, 0.5) OVER (). See APPROX_QUANTILES and PERCENTILE_CONT.

Errors you may hit

More aggregate functions

All BigQuery aggregate functions · All functions · 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.