Aggregate Functions

Aggregate functions operate on a column or column expression of a table or group (as created via GROUP BY clause).

For the examples below we use a simple table t(i int, c varchar(8)) with contents: INSERT into t(i,c) VALUES (1, 'one'), (2, 'two'), (3, 'tree'), (4, 'four'), (NULL, NULL); to create the shown Results.

Standard aggregate functions

FunctionReturn typeDescriptionExampleResult
avg(numeric_expr)doublecompute the average of the non-NULL integer/float/decimal/month_interval values in the column or groupavg(i)2.5
count(*)bigintcount the number of rows in the column or group including those who may have NULL valuescount(*)5
count(col_expr)bigintcount the number of non-NULL values in the column or groupcount(i)4
count(distinct col_expr)bigintcount the number of distinct non-NULL values in the column or groupcount(distinct i%2)2
max(col_expr)type of col_exprreturn the maximum non-NULL value in the column or groupmax(c)two
min(col_expr)type of col_exprreturn the minimum non-NULL value in the column or groupmin(c)four
prod(numeric_expr)bigint or hugeint or doublecompute the product of the non-NULL numeric values in the column or groupprod(i)24
prod(distinct numeric_expr)bigint or hugeint or doublecompute the product of the distinct non-NULL numeric values in the column or groupprod(distinct 1 + i%2)2
sum(integer_expr)bigint or hugeintcompute the sum of all non-NULL integer values in the column or groupsum(i)10
sum(distinct integer_expr)bigint or hugeintcompute the sum of all distinct non-NULL integer values in the column or groupsum(distinct i/2)3
sum(decimal_expr)decimalcompute the sum of all non-NULL decimal values in the column or groupsum(cast(i as decimal))10.000
sum(float_expr)real or doublecompute the sum of all non-NULL floating point values in the column or groupsum(cast(i as real))10.0
sum(month_interval_expr)month_intervalcompute the sum of all non-NULL interval month values in the column or groupsum(cast(i as interval month))10
sum(sec_interval_expr)sec_intervalcompute the sum of all non-NULL interval second values in the column or groupsum(cast(i as interval second))10.000

Aggregate functions returning a concatenated CLOB string

Note: You must include the sys. or json. prefix for these aggregate functions (see examples) for them to work properly.

FunctionReturn typeDescriptionExampleResult
json.tojsonarray(clob)varcharconvert string values in the column or group into a json array stringjson.tojsonarray(c)[ "one", "two", "tree", "four" ]
json.tojsonarray(double)varcharconvert numeric values in the column or group into a json array stringjson.tojsonarray(i)[ 1.000000, 2.000000, 3.000000, 4.000000 ]

For the examples below we add some data to the example table t: INSERT into t(i,c) VALUES (1, ''), (1, 'kwik'), (2, NULL), (2, 'kwek'), (3, ' '), (3, 'kwak'), (4, 'kwak'), (4, ' '); to create the shown Results.

FunctionReturn typeDescriptionExampleResult
sys.group_concat(col_expr)varcharconcatenate all non-NULL values in a column or group into one string with values separated by a comma (the default separator)sys.group_concat(c)kwek, ,kwak,kwak, ,one,two,tree,four,,kwik
sys.group_concat(ALL col_expr)varcharsame as sys.group_concat(col_expr), i.e. ALL is the default behavioursys.group_concat(ALL c)one,two,tree,four,,kwik,kwek, ,kwak,kwak,
sys.group_concat(DISTINCT col_expr)varcharconcatenate all non-NULL and unique values in a column or group into one string with values separated by a comma (the default separator)sys.group_concat(DISTINCT c)one,two,tree,four,,kwik,kwek, ,kwak
sys.group_concat(DISTINCT (col_expr))varcharsame as sys.group_concat(DISTINCT col_expr)sys.group_concat(DISTINCT (c))one,two,tree,four,,kwik,kwek, ,kwak
sys.group_concat(col_expr, separator_str)varcharconcatenate non-NULL values in a column or group into one string with values separated by separator_strsys.group_concat(i, '|')1|2|3|4|1|1|2|2|3|3|4|4
sys.group_concat(col_expr, separator_col_expr)varcharconcatenate non-NULL values in a column or group into one string with values separated by separator_col_exprsys.group_concat(i, c)1two2tree3four41kwik12kwek2 3kwak3kwak4 4
sys.group_concat(DISTINCT col_expr, separator_str)varcharconcatenate non-NULL and unique values in a column or group into one string with values separated by separator_strsys.group_concat(DISTINCT i, '|')1|2|3|4
sys.group_concat(DISTINCT col_expr, separator_col_expr)varcharconcatenate non-NULL values in a column or group into one string with values separated by separator_col_expr. DISTINCT is computed over (col_expr, separator_col_expr).sys.group_concat(DISTINCT i, c)1two2tree3four41kwik12kwek2 3kwak3kwak4 4
sys.group_concat(col_expr1 ORDER BY col_expr2)varcharconcatenate all non-NULL values in a column or group into one string with values separated by a comma (the default separator). Order the values of col_expr1 according to the ascending order (default) of the values of col_expr2sys.group_concat(c ORDER BY c), , ,four,kwak,kwak,kwek,kwik,one,tree,two
sys.group_concat(col_expr1 ORDER BY col_expr2 DESC)varcharconcatenate all non-NULL values in a column or group into one string with values separated by a comma (the default separator). Order the values of col_expr1 according to the descending order of the values of col_expr2sys.group_concat(c ORDER BY c DESC)two,tree,one,kwik,kwek,kwak,kwak,four, , ,
sys.group_concat(col_expr1) WITHIN GROUP (ORDER BY col_expr2)varcharsame as sys.group_concat(col_expr1 ORDER BY col_expr2)sys.group_concat(c) WITHIN GROUP (ORDER BY c), , ,four,kwak,kwak,kwek,kwik,one,tree,two
sys.group_concat(col_expr1, separator_str ORDER BY col_expr2)varcharconcatenate all non-NULL values in a column or group into one string with values separated by separator_str. Order the values of col_expr1 according to the ascending order of the values of col_expr2sys.group_concat(c, '|' ORDER BY c)| | |four|kwak|kwak|kwek|kwik|one|tree|two
sys.group_concat(DISTINCT col_expr1, separator_str ORDER BY col_expr2)varcharconcatenate all non-NULL and unique values in a column or group into one string with values separated by separator_str. Order the values of col_expr1 according to the ascending order of the values of col_expr2sys.group_concat(DISTINCT c, '|' ORDER BY c)| |four|kwak|kwek|kwik|one|tree|two

Note: for the GROUP_CONCAT examples above without an ORDER BY condition, we have shown only one of their correct result. Without an ordering, the input values can be concatinated in any order. In addition, depending on the implementation, every reexecution of the same function can produce a different but correct result. To demonstrate this, we show different results for sys.group_concat(c) and sys.group_concat(ALL c), even though they are essencially the same function.

Note: in MonetDB, LISTAGG is an equivalent of GROUP_CONCAT. To use LISTAGG, one simply replaces GROUP_CONCAT with LISTAGG in the above examples.

Aggregate functions for statistics

Note: You must include the sys. prefix for below aggregate functions (see examples) in order to work properly.

Statistic FunctionReturn typeDescriptionExampleResult
sys.corr(numeric_a, numeric_b)doublereturn the correlation coefficient of the non-NULL values in the numeric (integer/decimal/real/double) column or group numeric_a versus numeric_bsys.corr(i, i+2)0.7500000000000001
sys.covar_pop(numeric_a, numeric_b)doublereturn the population covariance of numeric_expr of the non-NULL values in the numeric (integer/decimal/real/double/interval) column or group numeric_a versus numeric_b. Supported from release Jun2020 (11.37.7)sys.covar_pop(i, i*2)2.5
sys.covar_samp(numeric_a, numeric_b)doublereturn the sample covariance of numeric_expr of the non-NULL values in the numeric (integer/decimal/real/double/interval) column or group numeric_a versus numeric_b. Supported from release Jun2020 (11.37.7)sys.covar_samp(i, i*2)3.3333333333333335
sys.median(col_expr)type of col_exprreturn the statistic median of the non-NULL values in the numeric (integer/decimal/real/double) or temporal (date/time/timestamp/interval) column or groupsys.median(i)2
sys.median_avg(numeric_expr)doublereturn the statistic median average of the non-NULL values in the numeric (integer/decimal/real/double) column or groupsys.median_avg(i)2.5
sys.quantile(col_expr, percentile)type of col_exprreturn the statistic quantile of the non-NULL values in the numeric (integer/decimal/real/double) or temporal (date/time/timestamp/interval) column or group. The percentile argument must be between 0.0 and 1.0sys.quantile(i, 0.7)3
sys.quantile_avg(numeric_expr, percentile)doublereturn the statistic quantile average of the non-NULL values in the numeric (integer/decimal/real/double) column or group. The percentile argument must be between 0.0 and 1.0sys.quantile_avg(i, 0.6)2.8
sys.stddev_pop(numeric_expr)doublereturn the population standard deviation of numeric_expr of the non-NULL values in the numeric (integer/decimal/real/double/interval) column or group. Same as the square root of var_pop().sys.stddev_pop(i)1.118033988749895
sys.stddev_samp(numeric_expr)doublereturn the sample standard deviation of numeric_expr of the non-NULL values in the numeric (integer/decimal/real/double/interval) column or group. Same as the square root of var_samp().sys.stddev_samp(i)1.2909944487358056
sys.var_pop(numeric_expr)doublereturn the population standard variance of numeric_expr of the non-NULL values in the numeric (integer/decimal/real/double/interval) column or group. Same as the square of stddev_pop().sys.var_pop(i)1.25
sys.var_samp(numeric_expr)doublereturn the sample standard variance of numeric_expr of the non-NULL values in the numeric (integer/decimal/real/double/interval) column or group. Same as the square of stddev_samp().sys.var_samp(i)1.6666666666666667

You can also create your own aggregate functions, see Create Aggregate function command.

To list all the available aggregate functions in your MonetDB server run query:

SELECT DISTINCT schema_id, name, func, mod, language, system FROM sys.functions WHERE type = 3 ORDER BY name;