View a markdown version of this page

Aggregate functions in DuckDB - Amazon Athena

Aggregate functions in DuckDB

Use aggregate functions to compute one value from many rows, such as a count, a total, an average, or the value from the row with the largest amount. Without a GROUP BY clause, an aggregate function reads every row that the query selects and returns one row. With GROUP BY, it returns one row for each group, computed over the rows of that group. For more information about grouping, see GROUP BY clause.

Most aggregate functions skip NULL input values. When every input value is NULL, or when there are no input rows, they return NULL. The count and approx_count_distinct functions return 0 instead. The count_if function returns NULL, like most aggregate functions. Some functions, such as arg_max_null and arg_max_nulls_last, keep NULL values on purpose, and their pages say so.

Inside the parentheses of an aggregate function call, DISTINCT passes each distinct value to the function once, and ORDER BY sets the order in which the function reads its input. Use ORDER BY with functions whose result depends on that order, such as any_value, first, list, and string_agg. After the call, FILTER (WHERE condition) limits the rows that one aggregate reads without changing the rows that the rest of the query reads. For more information about these clauses, see Aggregate function clauses.

The following table lists the aggregate functions, with a link to the description of each one.

Function Description

Aggregate function clauses

Describes the DISTINCT, ORDER BY, and FILTER clauses, which change the input rows of an aggregate function call or their order.

any_value

Returns a value from one of the input rows, skipping NULL values.

approx_count_distinct

Returns an estimate of the number of distinct non-NULL values.

approx_quantile

Returns an estimate of a quantile, or of several quantiles, of the input values.

approx_top_k

Returns an estimate of the k most frequent values, as a list.

arbitrary

Alias of first.

arg_max

Returns the value of an expression from the row that has the largest value of another expression.

arg_max_null

Returns the value of an expression from the row that has the largest value of another expression, even when that value is NULL.

arg_max_nulls_last

Returns the value of an expression from the rows with the largest values of another expression, with NULL values sorted last.

arg_min

Returns the value of an expression from the row that has the smallest value of another expression.

arg_min_null

Returns the value of an expression from the row that has the smallest value of another expression, even when that value is NULL.

arg_min_nulls_last

Returns the value of an expression from the rows with the smallest values of another expression, with NULL values sorted last.

argmax

Alias of arg_max.

argmin

Alias of arg_min.

array_agg

Alias of list.

avg

Returns the average of the non-NULL input values.

bit_and

Combines the non-NULL input values with a bitwise AND.

bit_or

Combines the non-NULL input values with a bitwise OR.

bit_xor

Combines the non-NULL input values with a bitwise exclusive OR (XOR).

bitstring_agg

Returns a BIT string that marks which integers in a range occur in the input.

bool_and

Tests whether a condition holds for all rows, ignoring NULL values.

bool_or

Tests whether a condition holds for at least one row, ignoring NULL values.

corr

Returns the correlation coefficient of two sets of numbers.

count

Counts the rows in a group, or the values that are not NULL.

count_if

Counts the rows for which a condition is true.

countif

Alias of count_if.

covar_pop

Returns the population covariance of two sets of numbers.

covar_samp

Returns the sample covariance of two sets of numbers.

decimal_average

Returns the average of DECIMAL values as a DECIMAL of the same type.

entropy

Returns the base-2 entropy of the distribution of values in a group.

favg

Returns the average of a set of numbers as a DOUBLE, and reduces the floating-point rounding error that builds up as the values are added.

first

Returns the value from the first row of a group, even when that value is NULL.

fsum

Alias of kahan_sum.

geomean

Returns the geometric mean of a set of positive numbers.

geometric_mean

Returns the geometric mean of a set of positive numbers, the same as geomean.

group_concat

Alias of string_agg.

histogram

Counts how often each value occurs, or how many values fall into each bin that you give, and returns the counts as a map.

histogram_exact

Counts how many values equal each bin that you give, and returns the counts as a map.

is_histogram_other_bin

Tests whether a value is the key of the extra bin in which histogram counts values outside the bins that you list.

kahan_sum

Returns the sum of a set of numbers as a DOUBLE, and reduces the floating-point rounding error that builds up as the values are added.

kurtosis

Returns the sample excess kurtosis of a set of numbers.

kurtosis_pop

Returns the population excess kurtosis of a set of numbers.

last

Returns the value from the last row of a group, in the order that you set with ORDER BY.

list

Collects the values of a group into a list.

listagg

Alias of string_agg.

lttb

Reduces a series of (x, y) points to a smaller set of points that keeps the shape of the series.

mad

Returns the median absolute deviation of a set of values.

max

Returns the largest value in a group, or a list of the largest values.

max_by

Alias of arg_max.

mean

Alias of avg.

median

Returns the median of a group, the value in the middle when the values are sorted.

min

Returns the smallest value in a group, or a list of the smallest values.

min_by

Alias of arg_min.

mode

Returns the value that occurs most often in a group.

product

Multiplies the values in a group and returns the product.

quantile

Alias of quantile_disc.

quantile_cont

Returns an interpolated quantile, such as a percentile, of a set of values.

quantile_disc

Returns a quantile, such as a percentile, of a set of values, chosen from the values themselves.

regr_avgx

Averages the independent variable x, using only the rows where neither argument is NULL.

regr_avgy

Averages the dependent variable y, using only the rows where neither argument is NULL.

regr_count

Returns the number of rows where neither the dependent nor the independent variable is NULL.

regr_intercept

Fits a least-squares line to the (x, y) pairs in a group and returns its y-intercept.

regr_r2

Fits a least-squares line to the (x, y) pairs in a group and returns its coefficient of determination (R squared), which shows how well the line fits.

regr_slope

Fits a least-squares line to the (x, y) pairs in a group and returns its slope.

regr_sxx

Returns the sum of the squared deviations of the independent variable from its mean, over the rows where neither variable is NULL.

regr_sxy

Returns the sum of the products of the deviations of two variables from their means, over the rows where neither variable is NULL.

regr_syy

Returns the sum of the squared deviations of the dependent variable from its mean, over the rows where neither variable is NULL.

reservoir_quantile

Estimates a quantile of the values in a group from a random sample of the values.

sem

Computes the standard error of the mean for the values in a group.

skewness

Measures how asymmetric the distribution of the values in a group is.

stddev

Alias of stddev_samp.

stddev_pop

Computes the population standard deviation of the values in a group.

stddev_samp

Computes the sample standard deviation of the values in a group.

string_agg

Joins the non-NULL values in a group into one string, with a separator between them.

sum

Adds up the non-NULL values in a group.

sumkahan

Alias of kahan_sum.

var_pop

Computes the population variance of the values in a group.

var_samp

Computes the sample variance of the values in a group.

variance

Alias of var_samp.

wavg

Alias of weighted_avg.

weighted_avg

Returns the average of the values in a group, with each value weighted by a second column.