6.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()
andapprox_distinct()
, all of these aggregate functions ignore null valuesand 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 nullvalues in the count. The coalesce
function can be used to convert null intozero.
Some aggregate functions such as array_agg()
produce different resultsdepending on the order of input values. This ordering can be specified by writingan ORDER BY Clause within the aggregate function:
- array_agg(x ORDER BY y DESC)
- array_agg(x ORDER BY x, y, z)
General Aggregate Functions
arbitrary
(x) → [same as input]Returns an arbitrary non-null value of
x
, if one exists.arrayagg
(_x) → array<[same as input]>Returns an array created from the input
x
elements.avg
(x) → doubleReturns the average (arithmetic mean) of all input values.
avg
(time interval type) → time interval typeReturns the average interval length of all input values.
booland
(_boolean) → booleanReturns
TRUE
if every input value isTRUE
, otherwiseFALSE
.boolor
(_boolean) → booleanReturns
TRUE
if any input value isTRUE
, otherwiseFALSE
.checksum
(x) → varbinaryReturns an order-insensitive checksum of the given values.
count
(*) → bigintReturns the number of input rows.
count
(x) → bigintReturns the number of non-null input values.
countif
(_x) → bigintReturns the number of
TRUE
input values.This function is equivalent tocount(CASE WHEN x THEN 1 END)
.every
(boolean) → booleanThis is an alias for
bool_and()
.geometricmean
(_x) → doubleReturns the geometric mean of all input values.
maxby
(_x, y) → [same as x]Returns the value of
x
associated with the maximum value ofy
over all input values.maxby
(_x, y, n) → array<[same as x]>Returns
n
values ofx
associated with then
largest of all input values ofy
in descending order ofy
.minby
(_x, y) → [same as x]Returns the value of
x
associated with the minimum value ofy
over all input values.minby
(_x, y, n) → array<[same as x]>Returns
n
values ofx
associated with then
smallest of all input values ofy
in ascending order ofy
.max
(x) → [same as input]Returns the maximum value of all input values.
max
(x, n) → array<[same as x]>Returns
n
largest 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
n
smallest values of all input values ofx
.reduceagg
(_inputValue T, initialState S, inputFunction(S, T, S), combineFunction(S, S, S)) → S- Reduces all input values into a single value.
`inputFunction
will be invokedfor each input value. In addition to taking the input value,inputFunction
takes the current state, initiallyinitialState
, and returns the new state.combineFunction
will 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, 2)
- (1, 3),
- (1, 4),
- (2, 20),
- (2, 30),
- (2, 40)
- ) AS t(id, value)
- GROUP BY id;
- -- (1, 9)
- -- (2, 90)
- SELECT id, reduce_agg(value, 1, (a, b) -> a * b, (a, b) -> a * b)
- FROM (
- VALUES
- (1, 2),
- (1, 3),
- (1, 4),
- (2, 20),
- (2, 30),
- (2, 40)
- ) AS t(id, value)
- GROUP BY id;
- -- (1, 24)
- -- (2, 24000)
The state type must be a boolean, integer, floating-point, or date/time/interval.
sum
(x) → [same as input]- Returns the sum of all input values.
Bitwise Aggregate Functions
bitwiseand_agg
(_x) → bigintReturns the bitwise AND of all input values in 2’s complement representation.
bitwiseor_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.
mapagg
(_key, value) -> map(K, V)Returns a map created from the input
key
/value
pairs.mapunion
(_x(K, V)) -> map(K, V)Returns the union of all the input maps. If a key is found in multipleinput maps, that key’s value in the resulting map comes from an arbitrary input map.
multimapagg
(_key, value) -> map(K, array(V))- Returns a multimap created from the input
key
/value
pairs.Each key can be associated with multiple values.
Approximate Aggregate Functions
approxdistinct
(_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 thestandard deviation of the (approximately normal) error distribution overall possible sets. It does not guarantee an upper bound on the error forany specific input set.
approxdistinct
(_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
, whichis the standard deviation of the (approximately normal) error distributionover all possible sets. It does not guarantee an upper bound on the errorfor any specific input set. The current implementation of this functionrequires that e
be in the range of [0.0040625, 0.26000]
.
approxpercentile
(_x, percentage) → [same as x]Returns the approximate percentile for all input values of
x
at thegivenpercentage
. The value ofpercentage
must be between zero andone and must be constant for all input rows.approxpercentile
(_x, percentages) → array<[same as x]>Returns the approximate percentile for all input values of
x
at each ofthe specified percentages. Each element of thepercentages
array must bebetween zero and one, and the array must be constant for all input rows.approxpercentile
(_x, w, percentage) → [same as x]Returns the approximate weighed percentile for all input values of
x
using the per-item weightw
at the percentagep
. The weight must bean integer value of at least one. It is effectively a replication count forthe valuex
in the percentile set. The value ofp
must be betweenzero and one and must be constant for all input rows.approxpercentile
(_x, w, percentage, accuracy) → [same as x]Returns the approximate weighed percentile for all input values of
x
using the per-item weightw
at the percentagep
, with a maximum rankerror ofaccuracy
. The weight must be an integer value of at least one.It is effectively a replication count for the valuex
in the percentileset. The value ofp
must be between zero and one and must be constantfor all input rows.accuracy
must be a value greater than zero and lessthan one, and it must be constant for all input rows.approxpercentile
(_x, w, percentages) → array<[same as x]>Returns the approximate weighed percentile for all input values of
x
using the per-item weightw
at each of the given percentages specifiedin the array. The weight must be an integer value of at least one. It iseffectively a replication count for the valuex
in the percentile set.Each element of the array must be between zero and one, and the array mustbe constant for all input rows.approxset
(_x) → HyperLogLogmerge
(x) → HyperLogLogmerge
(qdigest(T)) -> qdigest(T)qdigestagg
(_x) → qdigest<[same as x]>qdigestagg
(_x, w) → qdigest<[same as x]>qdigestagg
(_x, w, accuracy) → qdigest<[same as x]>numerichistogram
(_buckets, value, weight) → map- Computes an approximate histogram with up to
buckets
number of bucketsfor allvalue
s with a per-item weight ofweight
. The keys of thereturned map are roughly the center of the bin, and the entry is the totalweight of the bin. 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.
buckets
must be a bigint
. value
and weight
must be numeric.
numerichistogram
(_buckets, value) → map- Computes an approximate histogram with up to
buckets
number of bucketsfor allvalue
s. This function is equivalent to the variant ofnumeric_histogram()
that takes aweight
, with a per-item weight of1
.In this case, the total weight in the returned map is the count of items in the bin.
Statistical Aggregate Functions
corr
(y, x) → doubleReturns correlation coefficient of input values.
covarpop
(_y, x) → doubleReturns the population covariance of input values.
covarsamp
(_y, x) → doubleReturns the sample covariance of input values.
entropy
(c) → double- Returns the log-2 entropy of count input-values.
- entropy(c) = \sum_i [ c_i / \sum_j [c_j] \log_2(\sum_j [c_j] / c_i) ]
c
must be a bigint
column of non-negative values.
The function ignores any NULL
count. If the sum of non-NULL
counts is 0,it returns 0.
kurtosis
(x) → double- Returns the excess kurtosis of all input values. Unbiased estimate usingthe 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))
classificationmiss_rate
(_buckets, y, x, weight) → array- Computes the miss-rate part of the receiver operator curve with up to
buckets
number of buckets. Returnsan array of miss-rate values.y
should be a boolean outcome value;x
should be predictions, eachbetween 0 and 1;weight
should be non-negative values, indicating the weight of the instance.
To get an ROC map, use this in conjunction with classification_recall()
:
- MAP(classification_recall(200, outcome, prediction), classification_miss_rate(200, outcome, prediction))
classificationprecision
(_buckets, y, x) → array- This function is equivalent to the variant of
classification_precision()
that takes aweight
, with a per-item weight of1
. classificationprecision
(_buckets, y, x, weight) → array- Computes the precision part of the precision-recall curve with up to
buckets
number of buckets. Returnsan array of precision values.y
should be a boolean outcome value;x
should be predictions, eachbetween 0 and 1;weight
should be non-negative values, indicating the weight of the instance.
To get a map of recall to precision, use this in conjunction with classification_recall()
:
- MAP(classification_recall(200, outcome, prediction), classification_precision(200, outcome, prediction))
classificationprecision
(_buckets, y, x) → array- This function is equivalent to the variant of
classification_precision()
that takes aweight
, with a per-item weight of1
. classificationrecall
(_buckets, y, x, weight) → array- Computes the recall part of the precision-recall curve or the receiver operator charateristic curvewith up to
buckets
number of buckets. Returns an array of recall values.y
should be a boolean outcome value;x
should be predictions, eachbetween 0 and 1;weight
should be non-negative values, indicating the weight of the instance.
To get a map of recall to precision, use this in conjunction with classification_recall()
:
- MAP(classification_recall(200, outcome, prediction), classification_precision(200, outcome, prediction))
classificationrecall
(_buckets, y, x) → array- This function is equivalent to the variant of
classification_recall()
that takes aweight
, with a per-item weight of1
. classificationthresholds
(_buckets, y, x) → array- Computes the thresholds part of the precision-recall curve with up to
buckets
number of buckets. Returnsan array of thresholds.y
should be a boolean outcome value;x
should be predictions, eachbetween 0 and 1.
To get a map of thresholds to precision, use this in conjunction with classification_precision()
:
- MAP(classification_thresholds(200, outcome, prediction), classification_precision(200, outcome, prediction))
To get a map of thresholds to recall, use this in conjunction with classification_recall()
:
- MAP(classification_thresholds(200, outcome, prediction), classification_recall(200, outcome, prediction))
regrintercept
(_y, x) → doubleReturns linear regression intercept of input values.
y
is the dependentvalue.x
is the independent value.regrslope
(_y, x) → doubleReturns linear regression slope of input values.
y
is the dependentvalue.x
is the independent value.skewness
(x) → doubleReturns the skewness of all input values.
stddev
(x) → doubleThis is an alias for
stddev_samp()
.stddevpop
(_x) → doubleReturns the population standard deviation of all input values.
stddevsamp
(_x) → doubleReturns the sample standard deviation of all input values.
variance
(x) → doubleThis is an alias for
var_samp()
.varpop
(_x) → doubleReturns the population variance of all input values.
varsamp
(_x) → double- Returns the sample variance of all input values.