Aggregate Functions
Aggregate Functions
Aggregates summarize rows at the query's grain, or at an explicit by grain. For a walkthrough, see context-sensitive functions. Each example's Run and Copy includes the orders model.
SUM
Computes the sum of non-null numeric values within each group. Returns NULL if the group is empty or contains only NULL values.
COUNT
Counts the number of non-null input rows within each group. Takes a single argument of any type.
COUNT_DISTINCT
Counts the number of distinct non-null input values within each group. Takes a single argument of any type. Use this to count unique property values; a count of a key already has distinct-key semantics.
MAX
Returns the maximum non-null value across all input rows within each group. Comparison follows standard data type rules.
MIN
Returns the minimum non-null value across all input rows within each group. Comparison follows standard data type rules.
AVG
Computes the average (arithmetic mean) of non-null numeric values within each group. Returns NULL if the group is empty or contains only NULL values. The result is typically a FLOAT or NUMBER.
STDDEV
Computes the sample standard deviation of non-null numeric values. With fewer than two non-null values, the sample statistic is NULL.
VARIANCE
Computes the sample variance of non-null numeric values. With fewer than two non-null values, the sample statistic is NULL.
For customer 101's amounts of 120 and 80, sample variance is 800 and sample standard deviation is approximately 28.28. Customers 102 and 103 each have only one non-null amount, so both statistics are null.
ARRAY_AGG
Aggregates values from a group into a single array. The order of elements in the array is not guaranteed.
ANY
Returns a single, non-deterministic value from each group. Use it when any representative value is acceptable. It does not select the first or last value; use a window function when ordering matters.
BOOL_AND
Returns TRUE if every non-null input is TRUE, FALSE if any input is FALSE, and NULL if all values are NULL.
BOOL_OR
Returns TRUE if any non-null input is TRUE, FALSE if all non-null inputs are FALSE, and NULL if all values are NULL.