Aggregate functions

Updated at:

ApsaraDB for ClickHouse supports aggregate functions for statistical analysis, approximate computation, bitmap operations, and more. All NULL arguments are skipped during aggregation. Functions that accept multiple arguments ignore any row where at least one argument is NULL—except for first_value, last_value, any, and anyLast when used with the RESPECT NULLS modifier.

Click a function name to view its syntax, parameters, and examples in the ClickHouse documentation.

Basic aggregation

FunctionDescription
countCounts rows or non-NULL values
sumCalculates the sum of values
avgCalculates the arithmetic mean
minReturns the minimum value
maxReturns the maximum value
anyReturns an arbitrary value from the group. Alias: first_value
first_valueReturns the first encountered value. Alias of any
last_valueReturns the last encountered value. Alias of anyLast
anyHeavySelects a frequently occurring value using the heavy hitters algorithm
anyLastReturns the last value encountered. Alias: last_value
avgWeightedCalculates the weighted arithmetic mean
sumWithOverflowComputes the sum using the same data type as the input, allowing overflow
sumKahanComputes the sum using the Kahan compensated summation algorithm for higher floating-point accuracy
sumCountComputes the sum and count of non-NULL values simultaneously

Argument selection

FunctionDescription
argMinReturns the arg value corresponding to the minimum val
argMaxReturns the arg value corresponding to the maximum val
boundingRatioCalculates the slope between the minimum and maximum argument-value pairs

Statistical functions

FunctionDescription
stddevPopCalculates the population standard deviation
stddevSampCalculates the sample standard deviation
varSampCalculates the sample variance
covarPopCalculates the population covariance
covarSampCalculates the sample covariance
corrCalculates the Pearson correlation coefficient
rankCorrCalculates the rank correlation coefficient (Spearman's rho)
entropyCalculates the Shannon entropy of a column
skewPopCalculates the population skewness
skewSampCalculates the sample skewness
kurtPopCalculates the population kurtosis
kurtSampCalculates the sample kurtosis
simpleLinearRegressionPerforms simple (one-dimensional) linear regression
stochasticLinearRegressionImplements stochastic gradient descent for linear regression
stochasticLogisticRegressionImplements stochastic gradient descent for logistic regression (binary classification)
categoricalInformationValueCalculates the information value for a discrete (categorical) feature relative to a binary target
exponentialmovingaverageCalculates the exponential moving average of values at the given time
intervalLengthSumCalculates the total length of the union of intervals
deltaSumSums the positive differences between consecutive rows
deltaSumTimestampSums the positive differences between consecutive values, ordered by a timestamp column
sparkbarPlots a frequency histogram for values within a specified range

Statistical hypothesis testing

FunctionDescription
kolmogorovSmirnovTestApplies the Kolmogorov-Smirnov test to samples from two populations
studentTTestApplies Student's t-test to samples from two populations
welchTTestApplies Welch's t-test to samples from two populations
meanZTestApplies the mean z-test to samples from two populations
mannWhitneyUTestApplies the Mann-Whitney U test to samples from two populations

Association measures

FunctionDescription
contingencyCalculates the contingency coefficient, a measure of association between two columns
cramersVCalculates Cramer's V, a measure of association between two columns
cramersVBiasCorrectedCalculates a bias-corrected version of Cramer's V
theilsUCalculates Theil's U uncertainty coefficient, an asymmetric association measure

Approximate and top-K functions

FunctionDescription
topKReturns an array of the approximately most frequent values using the Space-Saving algorithm
topKWeightedReturns an array of the approximately most frequent values, weighted by a second column

Cardinality estimation

FunctionDescription
uniqEstimates the number of distinct values (fast, low memory)
uniqExactCounts the exact number of distinct values
uniqCombinedEstimates distinct values using a combined algorithm (array, hash table, HyperLogLog)
uniqCombined64Same as uniqCombined but uses a 64-bit hash, reducing collision probability for large datasets
uniqHLL12Estimates distinct values using the HyperLogLog algorithm with 2^12 cells
uniqThetaEstimates distinct values using the Theta Sketch Framework

Quantile and percentile functions

All quantile functions share a common family of variants. For example, quantile has corresponding quantiles, quantileExact, quantileExactWeighted, and other variants that trade accuracy for performance or accept weight columns.

FunctionDescription
quantileApproximates the quantile of a numeric data sequence
quantiles FunctionsComputes multiple quantiles in a single pass
quantileExact FunctionsComputes exact quantiles by fully sorting the data
quantileExactWeightedComputes exact quantiles with each element weighted by a count
quantileInterpolatedWeightedComputes quantiles using linear interpolation and weights
quantileGKApproximates quantiles using the Greenwald-Khanna algorithm with a guaranteed error bound
quantileTimingComputes quantiles of response time distributions with fixed precision
quantileTimingWeightedComputes weighted quantiles of response time distributions with fixed precision
quantileDeterministicApproximates quantiles deterministically using a reservoir sampling algorithm
quantileTDigestApproximates quantiles using the t-digest algorithm
quantileTDigestWeightedApproximates quantiles using the t-digest algorithm with element weights
quantileBFloat16Approximates quantiles using BFloat16 numbers for fast computation
medianAlias for the quantile family; computes the median (0.5 quantile)

Array aggregation

FunctionDescription
array_concat_aggConcatenates arrays from all rows into a single array
groupArrayCollects column values into an array
groupArrayLastCollects the last N column values into an array
groupUniqArrayCollects distinct column values into an array
groupArrayInsertAtInserts a value at the specified position in an array
groupArrayMovingSumCalculates the moving sum of column values
groupArrayMovingAvgCalculates the moving average of column values
groupArraySampleReturns a random sample of column values as an array

Bitwise aggregation

FunctionDescription
groupBitAndApplies bitwise AND across all values in a group
groupBitOrApplies bitwise OR across all values in a group
groupBitXorApplies bitwise XOR across all values in a group

Bitmap aggregation

FunctionDescription
groupBitmapBuilds a bitmap from a column of unsigned integers
groupBitmapAndComputes the AND (intersection) of bitmaps in a group
groupBitmapOrComputes the OR (union) of bitmaps in a group
groupBitmapXorComputes the XOR of bitmaps in a group

Map aggregation

FunctionDescription
sumMapSums map values by key across rows
minMapReturns the minimum map value per key across rows
maxMapReturns the maximum map value per key across rows

Interval aggregation

FunctionDescription
maxIntersectionsCalculates the maximum number of intersecting intervals
maxIntersectionsPositionReturns the starting positions where the maximum number of intervals intersect

Advanced features

The following entries link to advanced aggregate function capabilities rather than individual functions.

FeatureDescription
Aggregate Function CombinatorsSuffixes that modify aggregate function behavior (for example, -If, -Array, -State, -Merge)
Parametric Aggregate FunctionsAggregate functions that accept additional parameters alongside the column arguments
GROUPINGReturns 1 if a column is used in GROUP BY, and 0 if it represents an aggregated row (used with ROLLUP or CUBE)