Produce a single row containing the sum of the amount
column:
SELECT sum(amount) FROM sales;
Produce one row per unique region, containing the sum of amount
for each group:
SELECT region, sum(amount) FROM sales GROUP BY region;
Return only the regions that have a sum of amount
higher than 100:
SELECT region FROM sales GROUP BY region HAVING sum(amount) > 100;
Return the number of unique values in the region
column:
SELECT count(DISTINCT region) FROM sales;
Return two values, the total sum of amount
and the sum of amount
minus columns where the region is north
:
SELECT sum(amount), sum(amount) FILTER (region != 'north') FROM sales;
Returns a list of all regions in order of the amount
column:
SELECT list(region ORDER BY amount DESC) FROM sales;
Returns the amount of the first sale using the first()
aggregate function:
SELECT first(amount ORDER BY date ASC) FROM sales;
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.
An ORDER BY
clause can be provided after the last argument of the function call. Note the lack of the comma separator before the clause.
SELECT ⟨aggregate_function⟩(⟨arg⟩, ⟨sep⟩ ORDER BY ⟨ordering_criteria⟩);
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.
For example:
CREATE TABLE tbl AS SELECT s FROM range(1, 4) r(s);
SELECT string_agg(s, ', ' ORDER BY s DESC) AS countdown FROM tbl;
The table below shows the available general aggregate functions.
Description |
Returns the first non-null value from arg . This function is affected by ordering. |
Example |
any_value(A) |
Alias(es) |
- |
Description |
Returns the first value (null or non-null) from arg . This function is affected by ordering. |
Example |
arbitrary(A) |
Alias(es) |
first(A) |
Description |
Finds the row with the maximum val . Calculates the arg expression at that row. This function is affected by ordering. |
Example |
arg_max(A, B) |
Alias(es) |
argMax(arg, val) , max_by(arg, val) |
Description |
Finds the row with the minimum val . Calculates the arg expression at that row. This function is affected by ordering. |
Example |
arg_min(A, B) |
Alias(es) |
argMin(arg, val) , min_by(arg, val) |
Description |
Returns a LIST containing all the values of a column. This function is affected by ordering. |
Example |
array_agg(A) |
Alias(es) |
list |
Description |
Calculates the average value for all tuples in arg . |
Example |
avg(A) |
Alias(es) |
mean |
Description |
Returns the bitwise AND of all bits in a given expression. |
Example |
bit_and(A) |
Alias(es) |
- |
Description |
Returns the bitwise OR of all bits in a given expression. |
Example |
bit_or(A) |
Alias(es) |
- |
Description |
Returns the bitwise XOR of all bits in a given expression. |
Example |
bit_xor(A) |
Alias(es) |
- |
Description |
Returns a bitstring with bits set for each distinct value. |
Example |
bitstring_agg(A) |
Alias(es) |
- |
Description |
Returns true if every input value is true , otherwise false . |
Example |
bool_and(A) |
Alias(es) |
- |
Description |
Returns true if any input value is true , otherwise false . |
Example |
bool_or(A) |
Alias(es) |
- |
Description |
Calculates the number of tuples in arg . |
Example |
count(A) |
Alias(es) |
- |
Description |
Calculates the average using a more accurate floating point summation (Kahan Sum). |
Example |
favg(A) |
Alias(es) |
- |
Description |
Returns the first value (null or non-null) from arg . This function is affected by ordering. |
Example |
first(A) |
Alias(es) |
arbitrary(A) |
Description |
Calculates the sum using a more accurate floating point summation (Kahan Sum). |
Example |
fsum(A) |
Alias(es) |
sumKahan , kahan_sum |
Description |
Calculates the geometric mean for all tuples in arg . |
Example |
geomean(A) |
Alias(es) |
geometric_mean(A) |
Description |
Returns a MAP of key-value pairs representing buckets and counts. |
Example |
histogram(A) |
Alias(es) |
- |
Description |
Returns the last value of a column. This function is affected by ordering. |
Example |
last(A) |
Alias(es) |
- |
Description |
Returns a LIST containing all the values of a column. This function is affected by ordering. |
Example |
list(A) |
Alias(es) |
array_agg |
Description |
Returns the maximum value present in arg . |
Example |
max(A) |
Alias(es) |
- |
Description |
Finds the row with the maximum val . Calculates the arg expression at that row. This function is affected by ordering. |
Example |
max_by(A, B) |
Alias(es) |
argMax(arg, val) , arg_max(arg, val) |
Description |
Returns the minimum value present in arg . |
Example |
min(A) |
Alias(es) |
- |
Description |
Finds the row with the minimum val . Calculates the arg expression at that row. This function is affected by ordering. |
Example |
min_by(A, B) |
Alias(es) |
argMin(arg, val) , arg_min(arg, val) |
Description |
Calculates the product of all tuples in arg . |
Example |
product(A) |
Alias(es) |
- |
Description |
Concatenates the column string values with a separator. This function is affected by ordering. |
Example |
string_agg(S, ',') |
Alias(es) |
group_concat(arg, sep) , listagg(arg, sep) |
Description |
Calculates the sum value for all tuples in arg . |
Example |
sum(A) |
Alias(es) |
- |
Description |
Calculates the sum value for all tuples in arg without overflow checks. Unlike sum , which works on floating-point values, sum_no_overflow only accepts INTEGER and DECIMAL values. |
Example |
sum_no_overflow(A) |
Alias(es) |
- |
The table below shows the available approximate aggregate functions.
Function |
Description |
Example |
approx_count_distinct(x) |
Gives the approximate count of distinct elements using HyperLogLog. |
approx_count_distinct(A) |
approx_quantile(x, pos) |
Gives the approximate quantile using T-Digest. |
approx_quantile(A, 0.5) |
reservoir_quantile(x, quantile, sample_size = 8192) |
Gives the approximate quantile using reservoir sampling, the sample size is optional and uses 8192 as a default size. |
reservoir_quantile(A, 0.5, 1024) |
The table below shows the available statistical aggregate functions.
Function |
Description |
corr(y, x) |
Returns the correlation coefficient for non-null pairs in a group. |
covar_pop(y, x) |
Returns the population covariance of input values. |
covar_samp(y, x) |
Returns the sample covariance for non-null pairs in a group. |
entropy(x) |
Returns the log-2 entropy of count input-values. |
kurtosis_pop(x) |
Returns the excess kurtosis (Fisher's definition) of all input values. Bias correction is not applied. |
kurtosis(x) |
Returns the excess kurtosis (Fisher's definition) of all input values, with a bias correction according to the sample size. |
mad(x) |
Returns the median absolute deviation for the values within x. NULL values are ignored. Temporal types return a positive INTERVAL . |
median(x) |
Returns the middle value of the set. NULL values are ignored. For even value counts, quantitative values are averaged and ordinal values return the lower value. |
mode(x) |
Returns the most frequent value for the values within x. NULL values are ignored. |
quantile_cont(x, pos) |
Returns the interpolated pos -quantile of x for 0 <= pos <= 1 , i.e., orders the values of x and returns the pos * (n_nonnull_values - 1) th (zero-indexed) element (or an interpolation between the adjacent elements if the index is not an integer). If pos is a LIST of FLOAT s, then the result is a LIST of the corresponding interpolated quantiles. |
quantile_disc(x, pos) |
Returns the discrete pos -quantile of x for 0 <= pos <= 1 , i.e., orders the values of x and returns the floor(pos * (n_nonnull_values - 1)) th (zero-indexed) element. If pos is a LIST of FLOAT s, then the result is a LIST of the corresponding discrete quantiles. |
regr_avgx(y, x) |
Returns the average of the independent variable for non-null pairs in a group, where x is the independent variable and y is the dependent variable. |
regr_avgy(y, x) |
Returns the average of the dependent variable for non-null pairs in a group, where x is the independent variable and y is the dependent variable. |
regr_count(y, x) |
Returns the number of non-null number pairs in a group. |
regr_intercept(y, x) |
Returns the intercept of the univariate linear regression line for non-null pairs in a group. |
regr_r2(y, x) |
Returns the coefficient of determination for non-null pairs in a group. |
regr_slope(y, x) |
Returns the slope of the linear regression line for non-null pairs in a group. |
regr_sxx(y, x) |
- |
regr_sxy(y, x) |
Returns the population covariance of input values. |
regr_syy(y, x) |
- |
skewness(x) |
Returns the skewness of all input values. |
stddev_pop(x) |
Returns the population standard deviation. |
stddev_samp(x) |
Returns the sample standard deviation. |
var_pop(x) |
Returns the population variance. |
var_samp(x) |
Returns the sample variance of all input values. |
Description |
Returns the correlation coefficient for non-NULL pairs in a group. |
Formula |
covar_pop(y, x) / (stddev_pop(x) * stddev_pop(y)) |
Alias(es) |
- |
Description |
Returns the population covariance of input values. |
Formula |
(sum(x*y) - sum(x) * sum(y) / count(*)) / count(*) |
Alias(es) |
- |
Description |
Returns the sample covariance for non-NULL pairs in a group. |
Formula |
(sum(x*y) - sum(x) * sum(y) / count(*)) / (count(*) - 1) |
Alias(es) |
- |
Description |
Returns the log-2 entropy of count input-values. |
Formula |
- |
Alias(es) |
- |
Description |
Returns the excess kurtosis (Fisher's definition) of all input values. Bias correction is not applied. |
Formula |
- |
Alias(es) |
- |
Description |
Returns the excess kurtosis (Fisher's definition) of all input values, with a bias correction according to the sample size. |
Formula |
- |
Alias(es) |
- |
Description |
Returns the median absolute deviation for the values within x. NULL values are ignored. Temporal types return a positive INTERVAL . |
Formula |
median(abs(x - median(x))) |
Alias(es) |
- |
Description |
Returns the middle value of the set. NULL values are ignored. For even value counts, quantitative values are averaged and ordinal values return the lower value. |
Formula |
quantile_cont(x, 0.5) |
Alias(es) |
- |
Description |
Returns the most frequent value for the values within x. NULL values are ignored. |
Formula |
- |
Alias(es) |
- |
Description |
Returns the interpolated pos -quantile of x for 0 <= pos <= 1 , i.e., orders the values of x and returns the pos * (n_nonnull_values - 1) th (zero-indexed) element (or an interpolation between the adjacent elements if the index is not an integer). If pos is a LIST of FLOAT s, then the result is a LIST of the corresponding interpolated quantiles. |
Formula |
- |
Alias(es) |
- |
Description |
Returns the discrete pos -quantile of x for 0 <= pos <= 1 , i.e., orders the values of x and returns the floor(pos * (n_nonnull_values - 1)) th (zero-indexed) element. If pos is a LIST of FLOAT s, then the result is a LIST of the corresponding discrete quantiles. |
Formula |
- |
Alias(es) |
quantile |
Description |
Returns the average of the independent variable for non-null pairs in a group, where x is the independent variable and y is the dependent variable. |
Formula |
- |
Alias(es) |
- |
Description |
Returns the average of the dependent variable for non-null pairs in a group, where x is the independent variable and y is the dependent variable. |
Formula |
- |
Alias(es) |
- |
Description |
Returns the number of non-null number pairs in a group. |
Formula |
(sum(x*y) - sum(x) * sum(y) / count(*)) / count(*) |
Alias(es) |
- |
Description |
Returns the intercept of the univariate linear regression line for non-null pairs in a group. |
Formula |
avg(y) - regr_slope(y, x) * avg(x) |
Alias(es) |
- |
Description |
Returns the coefficient of determination for non-null pairs in a group. |
Formula |
- |
Alias(es) |
- |
Description |
Returns the slope of the linear regression line for non-null pairs in a group. |
Formula |
covar_pop(x, y) / var_pop(x) |
Alias(es) |
- |
Description |
- |
Formula |
regr_count(y, x) * var_pop(x) |
Alias(es) |
- |
Description |
Returns the population covariance of input values. |
Formula |
regr_count(y, x) * covar_pop(y, x) |
Alias(es) |
- |
Description |
- |
Formula |
regr_count(y, x) * var_pop(y) |
Alias(es) |
- |
Description |
Returns the skewness of all input values. |
Formula |
- |
Alias(es) |
- |
Description |
Returns the population standard deviation. |
Formula |
sqrt(var_pop(x)) |
Alias(es) |
- |
Description |
Returns the sample standard deviation. |
Formula |
sqrt(var_samp(x)) |
Alias(es) |
stddev(x) |
Description |
Returns the population variance. |
Formula |
- |
Alias(es) |
- |
Description |
Returns the sample variance of all input values. |
Formula |
(sum(x^2) - sum(x)^2 / count(x)) / (count(x) - 1) |
Alias(es) |
variance(arg, val) |
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)]) |
Function |
Description |
Alias |
grouping() |
For queries with GROUP BY and either ROLLUP or GROUPING SETS : Returns an integer identifying which of the argument expressions where used to group on to create the current supper-aggregate row. |
grouping_id() |