Request a demo

BigQuery reference

BigQuery aggregate functions

Aggregate functions collapse many rows into one value per group. In BigQuery most of them ignore NULLs, many accept DISTINCT, ORDER BY or LIMIT inside the call, and the APPROX_ functions trade a small error for much less memory.

Functions in this category

FunctionWhat it does
ANY_VALUEANY_VALUE returns the value of an expression from some row of the group, skipping NULLs.
APPROX_COUNT_DISTINCTAPPROX_COUNT_DISTINCT estimates the number of distinct non-NULL values with a HyperLogLog++ sketch.
APPROX_QUANTILESAPPROX_QUANTILES(x, n) returns an array of n + 1 approximate quantile boundaries, from the minimum to the maximum.
APPROX_TOP_COUNTAPPROX_TOP_COUNT(x, n) returns the n most frequent values of x with their approximate counts, as an ARRAY of STRUCT<value, count>.
ARRAY_AGGARRAY_AGG collects the values of a group into an ARRAY.
AVGAVG returns the mean of the non-NULL values in a group.
COUNTCOUNT counts rows: COUNT() counts every row, COUNT(column) skips NULLs, and COUNT(DISTINCT column) counts unique non-NULL values exactly.
COUNTIFCOUNTIF counts the rows where a BOOL condition is TRUE, for example COUNTIF(status = 'failed').
LOGICAL_ANDLOGICAL_AND returns TRUE if a BOOL expression is TRUE for every non-NULL row of a group and FALSE if any row is FALSE.
LOGICAL_ORLOGICAL_OR returns TRUE if a BOOL expression is TRUE for at least one row of a group and FALSE if every non-NULL row is FALSE.
MAXMAX returns the largest non-NULL value in a group: the latest timestamp, the highest amount, the alphabetically last string.
MAX_BYMAX_BY(x, y) returns the value of x from the row where y is largest, such as the current status of each order: MAX_BY(status, updated_at).
MINMIN returns the smallest non-NULL value in a group: the earliest date, the lowest price, the alphabetically first string.
MIN_BYMIN_BY(x, y) returns the value of x from the row where y is smallest, such as each user's first marketing channel: MIN_BY(channel, visited_at).
STRING_AGGSTRING_AGG concatenates the non-NULL strings of a group into one value, separated by a delimiter (a comma by default).
SUMSUM adds up the non-NULL values in a group and returns NULL, not 0, when there is nothing to add.

More of the BigQuery reference

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.