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
| Function | What it does |
|---|---|
| ANY_VALUE | ANY_VALUE returns the value of an expression from some row of the group, skipping NULLs. |
| APPROX_COUNT_DISTINCT | APPROX_COUNT_DISTINCT estimates the number of distinct non-NULL values with a HyperLogLog++ sketch. |
| APPROX_QUANTILES | APPROX_QUANTILES(x, n) returns an array of n + 1 approximate quantile boundaries, from the minimum to the maximum. |
| APPROX_TOP_COUNT | APPROX_TOP_COUNT(x, n) returns the n most frequent values of x with their approximate counts, as an ARRAY of STRUCT<value, count>. |
| ARRAY_AGG | ARRAY_AGG collects the values of a group into an ARRAY. |
| AVG | AVG returns the mean of the non-NULL values in a group. |
| COUNT | COUNT counts rows: COUNT() counts every row, COUNT(column) skips NULLs, and COUNT(DISTINCT column) counts unique non-NULL values exactly. |
| COUNTIF | COUNTIF counts the rows where a BOOL condition is TRUE, for example COUNTIF(status = 'failed'). |
| LOGICAL_AND | LOGICAL_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_OR | LOGICAL_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. |
| MAX | MAX returns the largest non-NULL value in a group: the latest timestamp, the highest amount, the alphabetically last string. |
| MAX_BY | MAX_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). |
| MIN | MIN returns the smallest non-NULL value in a group: the earliest date, the lowest price, the alphabetically first string. |
| MIN_BY | MIN_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_AGG | STRING_AGG concatenates the non-NULL strings of a group into one value, separated by a delimiter (a comma by default). |
| SUM | SUM 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
Date functionsDatetime functionsTimestamp functionsString functionsWindow functionsArray functionsJSON functionsConversion functionsConditional expressionsMathematical functionsHash and utility functionsQuery syntaxDDL statementsDML statementsScripting and data movementINFORMATION_SCHEMAError messagesBigQuery pricingPricing calculator
