Functions that compute a single result from a set of input values. Use aggregate functions to:
Summarize data by computing counts, sums, and averages.
Analyze distributions with statistical functions such as standard deviation and variance.
Find extremes with MIN and MAX values.
Compute percentiles and identify modes in datasets.
Considerations
Null handling: Except for COUNT, aggregate functions return NULL when no rows match. Use COALESCE(aggregate_func(...), default_value) to substitute a default value.
Group by requirement: Non-aggregated fields in the SELECT clause must appear in the GROUP BY clause.
Filter clause: Use the FILTER clause to apply conditions to individual aggregates.
General-Purpose Aggregates
Common aggregate functions for summarizing data:
ANY_VALUE - Return an arbitrary value from the group.
Cast the input of an aggregate function to force a different output type. For example, use VAR_POP(CAST(A AS DOUBLE PRECISION)) to get a double precision result.
Tip
Ordered-Set Aggregates
Aggregate functions that use the WITHIN GROUP syntax:
FIRST_VALUE - Return the <value> from the first row within each GROUP BY group after ordering by specified keys.
PERCENTILE_CONT - Compute continuous percentile with interpolation.
PERCENTILE_DISC - Compute discrete percentile without interpolation.
For MODE, PERCENTILE_CONT, and PERCENTILE_DISC, NULL values in the sorted input are ignored where noted on each function page. Percentile functions require a fraction between 0 and 1. FIRST_VALUE does not use a fraction. Ordering follows the ORDER BY keys and NULLS FIRST / NULLS LAST modifiers.
Special Functions and Clauses
GROUPING - Identify which columns are aggregated in grouping sets.
FILTER Clause - Filter rows passed to aggregate functions.