7.14. Aggregate Functions#
Aggregate functions operate on a set of values to compute a single result.
Except for count(), count_if(), max_by(), min_by() and
approx_distinct(), all of these aggregate functions ignore null values
and return null for no input rows or when all values are null. For example,
sum() returns null rather than zero and avg() does not include null
values in the count. The coalesce function can be used to convert null into
zero.
Ordering During Aggregation#
Some aggregate functions such as array_agg() produce different results
depending on the order of input values. This ordering can be specified by writing
an ORDER BY Clause within the aggregate function:
array_agg(x ORDER BY y DESC)
array_agg(x ORDER BY x, y, z)
Filtering During Aggregation#
The FILTER keyword can be used to remove rows from aggregation processing
with a condition expressed using a WHERE clause. This is evaluated for each
row before it is used in the aggregation and is supported for all aggregate
functions.
aggregate_function(...) FILTER (WHERE <condition>)
A common and very useful example is to use FILTER to remove nulls from
consideration when using array_agg:
SELECT array_agg(name) FILTER (WHERE name IS NOT NULL)
FROM region;
As another example, imagine you want to add a condition on the count for Iris flowers, modifying the following query:
SELECT species,
count(*) AS count
FROM iris
GROUP BY species;
species | count
-----------+-------
setosa | 50
virginica | 50
versicolor | 50
If you just use a normal WHERE statement you loose information:
SELECT species,
count(*) AS count
FROM iris
WHERE petal_length_cm > 4
GROUP BY species;
species | count
-----------+-------
virginica | 50
versicolor | 34
Using a filter you retain all information:
SELECT species,
count(*) FILTER (where petal_length_cm > 4) AS count
FROM iris
GROUP BY species;
species | count
-----------+-------
virginica | 50
setosa | 0
versicolor | 34
General Aggregate Functions#
-
arbitrary(x) → [same as input]# Returns an arbitrary non-null value of
x, if one exists.
-
array_agg(x) → array<[same as input]># Returns an array created from the input
xelements.
-
avg(x) → double# Returns the average (arithmetic mean) of all input values.
-
avg(time interval type) → time interval type Returns the average interval length of all input values.
-
bool_and(boolean) → boolean# Returns
TRUEif every input value isTRUE, otherwiseFALSE.
-
bool_or(boolean) → boolean# Returns
TRUEif any input value isTRUE, otherwiseFALSE.
-
checksum(x) → varbinary# Returns an order-insensitive checksum of the given values.
-
count(*) → bigint# Returns the number of input rows.
-
count(x) → bigint Returns the number of non-null input values.
-
count_if(x) → bigint# Returns the number of
TRUEinput values. This function is equivalent tocount(CASE WHEN x THEN 1 END).
-
every(boolean) → boolean# This is an alias for
bool_and().
-
geometric_mean(x) → double# Returns the geometric mean of all input values.
-
max_by(x, y) → [same as x]# Returns the value of
xassociated with the maximum value ofyover all input values.
-
max_by(x, y, n) → array<[same as x]> Returns
nvalues ofxassociated with thenlargest of all input values ofyin descending order ofy.
-
min_by(x, y) → [same as x]# Returns the value of
xassociated with the minimum value ofyover all input values.
-
min_by(x, y, n) → array<[same as x]> Returns
nvalues ofxassociated with thensmallest of all input values ofyin ascending order ofy.
-
max(x) → [same as input]# Returns the maximum value of all input values.
-
max(x, n) → array<[same as x]> Returns
nlargest values of all input values ofx.
-
min(x) → [same as input]# Returns the minimum value of all input values.
-
min(x, n) → array<[same as x]> Returns
nsmallest values of all input values ofx.
-
sum(x) → [same as input]# Returns the sum of all input values.
Bitwise Aggregate Functions#
-
bitwise_and_agg(x) → bigint# Returns the bitwise AND of all input values in 2’s complement representation.
-
bitwise_or_agg(x) → bigint# Returns the bitwise OR of all input values in 2’s complement representation.
Map Aggregate Functions#
-
histogram(x) -> map(K, bigint)# Returns a map containing the count of the number of times each input value occurs.
-
map_agg(key, value) -> map(K, V)# Returns a map created from the input
key/valuepairs.
-
map_union(x(K, V)) -> map(K, V)# Returns the union of all the input maps. If a key is found in multiple input maps, that key’s value in the resulting map comes from an arbitrary input map.
-
multimap_agg(key, value) -> map(K, array(V))# Returns a multimap created from the input
key/valuepairs. Each key can be associated with multiple values.
Approximate Aggregate Functions#
-
approx_distinct(x) → bigint# Returns the approximate number of distinct input values. This function provides an approximation of
count(DISTINCT x). Zero is returned if all input values are null.This function should produce a standard error of 2.3%, which is the standard deviation of the (approximately normal) error distribution over all possible sets. It does not guarantee an upper bound on the error for any specific input set.
-
approx_distinct(x, e) → bigint Returns the approximate number of distinct input values. This function provides an approximation of
count(DISTINCT x). Zero is returned if all input values are null.This function should produce a standard error of no more than
e, which is the standard deviation of the (approximately normal) error distribution over all possible sets. It does not guarantee an upper bound on the error for any specific input set. The current implementation of this function requires thatebe in the range of[0.0040625, 0.26000].
-
approx_percentile(x, percentage) → [same as x]# Returns the approximate percentile for all input values of
xat the givenpercentage. The value ofpercentagemust be between zero and one and must be constant for all input rows.
-
approx_percentile(x, percentages) → array<[same as x]> Returns the approximate percentile for all input values of
xat each of the specified percentages. Each element of thepercentagesarray must be between zero and one, and the array must be constant for all input rows.
-
approx_percentile(x, w, percentage) → [same as x] Returns the approximate weighed percentile for all input values of
xusing the per-item weightwat the percentagep. Weights must be strictly positive. Integer-value weights can be thought of as a replication count for the valuexin the percentile set. The value ofpmust be between zero and one and must be constant for all input rows.
-
approx_percentile(x, w, percentage, accuracy) → [same as x] Returns the approximate weighed percentile for all input values of
xusing the per-item weightwat the percentagep, with a maximum rank error ofaccuracy. Weights must be strictly positive. Integer-value weights can be thought of as a replication count for the valuexin the percentile set. The value ofpmust be between zero and one and must be constant for all input rows.accuracymust be a value greater than zero and less than one, and it must be constant for all input rows.
-
approx_percentile(x, w, percentages) → array<[same as x]> Returns the approximate weighed percentile for all input values of
xusing the per-item weightwat each of the given percentages specified in the array. Weights must be strictly positive. Integer-value weights can be thought of as a replication count for the valuexin the percentile set. Each element of the array must be between zero and one, and the array must be constant for all input rows.
-
approx_set(x) → HyperLogLog
-
merge(x) → HyperLogLog
-
merge(qdigest(T)) -> qdigest(T)
-
qdigest_agg(x) → qdigest<[same as x]>
-
qdigest_agg(x, w) → qdigest<[same as x]>
-
qdigest_agg(x, w, accuracy) → qdigest<[same as x]>
-
numeric_histogram(buckets, value, weight) → map<double, double># Computes an approximate histogram with up to
bucketsnumber of buckets for allvalues with a per-item weight ofweight. The algorithm is based loosely on:Yael Ben-Haim and Elad Tom-Tov, "A streaming parallel decision tree algorithm", J. Machine Learning Research 11 (2010), pp. 849--872.
bucketsmust be abigint.valueandweightmust be numeric.
-
numeric_histogram(buckets, value) → map<double, double> Computes an approximate histogram with up to
bucketsnumber of buckets for allvalues. This function is equivalent to the variant ofnumeric_histogram()that takes aweight, with a per-item weight of1.
Statistical Aggregate Functions#
-
corr(y, x) → double# Returns correlation coefficient of input values.
-
covar_pop(y, x) → double# Returns the population covariance of input values.
-
covar_samp(y, x) → double# Returns the sample covariance of input values.
-
kurtosis(x) → double# Returns the excess kurtosis of all input values. Unbiased estimate using the following expression:
kurtosis(x) = n(n+1)/((n-1)(n-2)(n-3))sum[(x_i-mean)^4]/stddev(x)^4-3(n-1)^2/((n-2)(n-3))
-
regr_intercept(y, x) → double# Returns linear regression intercept of input values.
yis the dependent value.xis the independent value.
-
regr_slope(y, x) → double# Returns linear regression slope of input values.
yis the dependent value.xis the independent value.
-
skewness(x) → double# Returns the skewness of all input values.
-
stddev(x) → double# This is an alias for
stddev_samp().
-
stddev_pop(x) → double# Returns the population standard deviation of all input values.
-
stddev_samp(x) → double# Returns the sample standard deviation of all input values.
-
variance(x) → double# This is an alias for
var_samp().
-
var_pop(x) → double# Returns the population variance of all input values.
-
var_samp(x) → double# Returns the sample variance of all input values.
Lambda Aggregate Functions#
-
reduce_agg(inputValue T, initialState S, inputFunction(S, T, S), combineFunction(S, S, S)) → S# Reduces all input values into a single value.
inputFunctionwill be invoked for each non-null input value. In addition to taking the input value,inputFunctiontakes the current state, initiallyinitialState, and returns the new state.combineFunctionwill be invoked to combine two states into a new state. The final state is returned:SELECT id, reduce_agg(value, 0, (a, b) -> a + b, (a, b) -> a + b) FROM ( VALUES (1, 3), (1, 4), (1, 5), (2, 6), (2, 7) ) AS t(id, value) GROUP BY id; -- (1, 12) -- (2, 13) SELECT id, reduce_agg(value, 1, (a, b) -> a * b, (a, b) -> a * b) FROM ( VALUES (1, 3), (1, 4), (1, 5), (2, 6), (2, 7) ) AS t(id, value) GROUP BY id; -- (1, 60) -- (2, 42)
The state type must be a boolean, integer, floating-point, or date/time/interval.