Aggregate Functions
Aggregate functions perform calculations across a set of input rows and return a single summary value for each group. Aggregate functions can either have an explicit grain - SUM(x) by a,b,c, - or be unbound, in which case they will inherit the grain from the surrounding expression. Typically, aggregates should be unbound in models except where they represent a specific property- for example, total_revenue would be dynamic, whereas 'customer_lifetime_revenue' might be a aggregate at a customer grain, an implicit property of a customer id.
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. You ONLY need to use this function when counting unique values of properties; a count of a key is always distinct. In most code this function is not required.
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.
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. This is useful when you know all values in a group are the same for a given field.
BOOL_AND
Returns TRUE if every input value in the group is TRUE. Returns FALSE if any value is FALSE, and NULL if all values are NULL.
BOOL_OR
Returns TRUE if at least one input value in the group is TRUE. Returns FALSE if all values are FALSE, and NULL if all values are NULL.
