Aggregates are functions that combine multiple rows into a single value. Aggregates are different from scalar functions and window functions because they change the cardinality of the result. As such, aggregates can only be used in the SELECT and HAVING clauses of a SQL query.
When the DISTINCT clause is provided, only distinct values are considered in the computation of the aggregate. This is typically used in combination with the count aggregate to get the number of distinct elements; but it can be used together with any aggregate function in the system.
There are some aggregates that are insensitive to duplicate values (e.g., min and max) and for them this clause is parsed and ignored.
This clause ensures that the values being aggregated are sorted before applying the function.
Most aggregate functions are order-insensitive, and for them this clause is parsed and discarded.
However, there are some order-sensitive aggregates that can have non-deterministic results without ordering, e.g., first, last, list and string_agg / group_concat / listagg.
These can be made deterministic by ordering the arguments.
All general aggregate functions except count return NULL on empty groups.
In particular, list does not return an empty list, sum does not return zero, and string_agg does not return an empty string in this case.
Finds the row with the maximum val and calculates the arg expression at that row. Rows where the value of the arg or val expression is NULL are ignored. This function is affected by ordering.
The generalized case of arg_max for n values: returns a LIST containing the arg expressions for the top n rows ordered by val descending. This function is affected by ordering.
Finds the row with the maximum val and calculates the arg expression at that row. Rows where the val expression evaluates to NULL are ignored. This function is affected by ordering.
Finds the row with the minimum val and calculates the arg expression at that row. Rows where the value of the arg or val expression is NULL are ignored. This function is affected by ordering.
Finds the row with the minimum val and calculates the arg expression at that row. Rows where the val expression evaluates to NULL are ignored. This function is affected by ordering.
Returns a MAP of key-value pairs representing the provided upper boundaries and counts of elements in the corresponding bins (left-open and right-closed partitions) of the datatype. A boundary at the largest value of the datatype is automatically added when elements larger than all provided boundaries appear, see is_histogram_other_bin. Boundaries may be provided, e.g., via equi_width_bins.
Returns a MAP of key-value pairs representing the requested elements and their counts. A catch-all element specific to the data-type is automatically added to count other elements when they appear, see is_histogram_other_bin.
Calculates the sum of all non-null values in arg / counts true values when arg is boolean. The floating-point versions of this function are affected by ordering.
Calculates the weighted average of all non-null values in arg, where each value is scaled by its corresponding weight. If weight is NULL, the corresponding arg value will be skipped. This function is affected by ordering.
Finds the row with the maximum val and calculates the arg expression at that row. Rows where the value of the arg or val expression is NULL are ignored. This function is affected by ordering.
The generalized case of arg_max for n values: returns a LIST containing the arg expressions for the top n rows ordered by val descending. This function is affected by ordering.
Finds the row with the maximum val and calculates the arg expression at that row. Rows where the val expression evaluates to NULL are ignored. This function is affected by ordering.
Finds the row with the minimum val and calculates the arg expression at that row. Rows where the value of the arg or val expression is NULL are ignored. This function is affected by ordering.
The generalized case of arg_min for n values: returns a LIST containing the arg expressions for the bottom n rows ordered by val ascending. This function is affected by ordering.
Finds the row with the minimum val and calculates the arg expression at that row. Rows where the val expression evaluates to NULL are ignored. This function is affected by ordering.
Returns a MAP of key-value pairs representing the provided upper boundaries and counts of elements in the corresponding bins (left-open and right-closed partitions) of the datatype. A boundary at the largest value of the datatype is automatically added when elements larger than all provided boundaries appear, see is_histogram_other_bin. Boundaries may be provided, e.g., via equi_width_bins.
Returns a MAP of key-value pairs representing the requested elements and their counts. A catch-all element specific to the data-type is automatically added to count other elements when they appear, see is_histogram_other_bin.
Calculates the sum of all non-null values in arg / counts true values when arg is boolean. The floating-point versions of this function are affected by ordering.
Calculates the weighted average of all non-null values in arg, where each value is scaled by its corresponding weight. If weight is NULL, the value will be skipped. This function is affected by ordering.
The table below shows the available statistical aggregate functions.
They all ignore NULL values (in the case of a single input column x), or pairs where either input is NULL (in the case of two input columns y and x).
The interpolated pos-quantile of x for -1 <= pos <= 1. Returns the pos * (n_nonnull_values - 1)th (zero-indexed, in the specified order) value of x or an interpolation between the adjacent values if the index is not an integer. Values of pos between -1 and 0 correspond to counting backwards from 1. More precisely, quantile_cont(x, -y) = quantile_cont(x, 1 - y). Intuitively, arranges the values of x as equispaced points on a line, starting at 0 and ending at 1, and returns the (interpolated) value at pos. This is Type 7 in Hyndman & Fan (1996). If pos is a LIST of FLOATs, then the result is a LIST of the corresponding interpolated quantiles.
The discrete pos-quantile of x for 0 <= pos <= 1. Returns the greatest(ceil(pos * n_nonnull_values) - 1, 0)th (zero-indexed, in the specified order) value of x. Intuitively, assigns to each value of x an equisized sub-interval (left-open and right-closed except for the initial interval) of the interval [0, 1], and picks the value of the sub-interval that contains pos. This is Type 1 in Hyndman & Fan (1996). If pos is a LIST of FLOATs, then the result is a LIST of the corresponding discrete quantiles.
The squared Pearson correlation coefficient between y and x. Also: The coefficient of determination in a linear regression, where x is the independent variable and y is the dependent variable.
The sample variance, which includes Bessel's bias correction, of the independent variable for non-NULL pairs, where x is the independent variable and y is the dependent variable.
The sample variance, which includes Bessel's bias correction, of the dependent variable for non-NULL pairs, where x is the independent variable and y is the dependent variable.
The interpolated pos-quantile of x for 0 <= pos <= 1. Returns the pos * (n_nonnull_values - 1)th (zero-indexed, in the specified order) value of x or an interpolation between the adjacent values if the index is not an integer. Intuitively, arranges the values of x as equispaced points on a line, starting at 0 and ending at 1, and returns the (interpolated) value at pos. This is Type 7 in Hyndman & Fan (1996). If pos is a LIST of FLOATs, then the result is a LIST of the corresponding interpolated quantiles.
The discrete pos-quantile of x for 0 <= pos <= 1. Returns the greatest(ceil(pos * n_nonnull_values) - 1, 0)th (zero-indexed, in the specified order) value of x. Intuitively, assigns to each value of x an equisized sub-interval (left-open and right-closed except for the initial interval) of the interval [0, 1], and picks the value of the sub-interval that contains pos. This is Type 1 in Hyndman & Fan (1996). If pos is a LIST of FLOATs, then the result is a LIST of the corresponding discrete quantiles.
The squared Pearson correlation coefficient between y and x. Also: The coefficient of determination in a linear regression, where x is the independent variable and y is the dependent variable.
The sample variance, which includes Bessel's bias correction, of the independent variable for non-NULL pairs, where x is the independent variable and y is the dependent variable.
The sample variance, which includes Bessel's bias correction, of the dependent variable for non-NULL pairs, where x is the independent variable and y is the dependent variable.
The table below shows the available “ordered set” aggregate functions.
These functions are specified using the WITHIN GROUP (ORDER BY sort_expression) syntax,
and they are converted to an equivalent aggregate function that takes the ordering expression
as the first argument.
Function
Equivalent
mode() WITHIN GROUP (ORDER BY column [(ASC|DESC)])
mode(column ORDER BY column [(ASC|DESC)])
percentile_cont(fraction) WITHIN GROUP (ORDER BY column [(ASC|DESC)])
quantile_cont(column, fraction ORDER BY column [(ASC|DESC)])
percentile_cont(fractions) WITHIN GROUP (ORDER BY column [(ASC|DESC)])
quantile_cont(column, fractions ORDER BY column [(ASC|DESC)])
percentile_disc(fraction) WITHIN GROUP (ORDER BY column [(ASC|DESC)])
quantile_disc(column, fraction ORDER BY column [(ASC|DESC)])
percentile_disc(fractions) WITHIN GROUP (ORDER BY column [(ASC|DESC)])
quantile_disc(column, fractions ORDER BY column [(ASC|DESC)])
For queries with GROUP BY and either ROLLUP or GROUPING SETS: Returns an integer identifying which of the argument expressions were used to group on to create the current super-aggregate row.