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 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.condition)
The following table lists the aggregate functions, with a link to the description of each one.
| Function | Description |
|---|---|
Describes the |
|
Returns a value from one of the input rows, skipping |
|
Returns an estimate of the number of distinct non- |
|
Returns an estimate of a quantile, or of several quantiles, of the input values. |
|
Returns an estimate of the |
|
arbitrary |
Alias of first. |
Returns the value of an expression from the row that has the largest value of another expression. |
|
Returns the value of an expression from the row that has the largest value of another expression, even when that value is |
|
Returns the value of an expression from the rows with the largest values of another expression, with |
|
Returns the value of an expression from the row that has the smallest value of another expression. |
|
Returns the value of an expression from the row that has the smallest value of another expression, even when that value is |
|
Returns the value of an expression from the rows with the smallest values of another expression, with |
|
argmax |
Alias of arg_max. |
argmin |
Alias of arg_min. |
array_agg |
Alias of list. |
Returns the average of the non- |
|
Combines the non- |
|
Combines the non- |
|
Combines the non- |
|
Returns a |
|
Tests whether a condition holds for all rows, ignoring |
|
Tests whether a condition holds for at least one row, ignoring |
|
Returns the correlation coefficient of two sets of numbers. |
|
Counts the rows in a group, or the values that are not |
|
Counts the rows for which a condition is true. |
|
countif |
Alias of count_if. |
Returns the population covariance of two sets of numbers. |
|
Returns the sample covariance of two sets of numbers. |
|
Returns the average of |
|
Returns the base-2 entropy of the distribution of values in a group. |
|
Returns the average of a set of numbers as a |
|
Returns the value from the first row of a group, even when that value is |
|
fsum |
Alias of kahan_sum. |
Returns the geometric mean of a set of positive numbers. |
|
Returns the geometric mean of a set of positive numbers, the same as |
|
group_concat |
Alias of string_agg. |
Counts how often each value occurs, or how many values fall into each bin that you give, and returns the counts as a map. |
|
Counts how many values equal each bin that you give, and returns the counts as a map. |
|
Tests whether a value is the key of the extra bin in which |
|
Returns the sum of a set of numbers as a |
|
Returns the sample excess kurtosis of a set of numbers. |
|
Returns the population excess kurtosis of a set of numbers. |
|
Returns the value from the last row of a group, in the order that you set with |
|
Collects the values of a group into a list. |
|
listagg |
Alias of string_agg. |
Reduces a series of ( |
|
Returns the median absolute deviation of a set of values. |
|
Returns the largest value in a group, or a list of the largest values. |
|
max_by |
Alias of arg_max. |
mean |
Alias of avg. |
Returns the median of a group, the value in the middle when the values are sorted. |
|
Returns the smallest value in a group, or a list of the smallest values. |
|
min_by |
Alias of arg_min. |
Returns the value that occurs most often in a group. |
|
Multiplies the values in a group and returns the product. |
|
quantile |
Alias of quantile_disc. |
Returns an interpolated quantile, such as a percentile, of a set of values. |
|
Returns a quantile, such as a percentile, of a set of values, chosen from the values themselves. |
|
Averages the independent variable |
|
Averages the dependent variable |
|
Returns the number of rows where neither the dependent nor the independent variable is |
|
Fits a least-squares line to the ( |
|
Fits a least-squares line to the ( |
|
Fits a least-squares line to the ( |
|
Returns the sum of the squared deviations of the independent variable from its mean, over the rows where neither variable is |
|
Returns the sum of the products of the deviations of two variables from their means, over the rows where neither variable is |
|
Returns the sum of the squared deviations of the dependent variable from its mean, over the rows where neither variable is |
|
Estimates a quantile of the values in a group from a random sample of the values. |
|
Computes the standard error of the mean for the values in a group. |
|
Measures how asymmetric the distribution of the values in a group is. |
|
stddev |
Alias of stddev_samp. |
Computes the population standard deviation of the values in a group. |
|
Computes the sample standard deviation of the values in a group. |
|
Joins the non- |
|
Adds up the non- |
|
sumkahan |
Alias of kahan_sum. |
Computes the population variance of the values in a group. |
|
Computes the sample variance of the values in a group. |
|
variance |
Alias of var_samp. |
wavg |
Alias of weighted_avg. |
Returns the average of the values in a group, with each value weighted by a second column. |