Skip to main content
Aggregate functions collapse many rows into a single value per group. In Segment Lists you typically pair them with GROUP BY distinct_id and a HAVING clause so the list keeps only the users whose aggregated activity matches your criteria.

count / count(distinct)

count(*) counts all rows in the group, count(col) counts non-null values, and count(distinct col) counts distinct non-null values (exact). Syntax
Arguments
  • col — the expression to count; omit (use *) to count rows.
Returned value
  • The count, as an integer.
Example
Total and distinct event counts per active user

sum

Returns the sum of the non-null numeric input values. Syntax
Arguments
  • col — the numeric expression to add up.
Returned value
  • The sum; its type follows the input’s numeric type.
Example
High-spend users

avg

Returns the arithmetic mean of the non-null numeric input values. Syntax
Arguments
  • col — the numeric expression to average.
Returned value
  • The mean, as a floating-point number.
Example
Users with a high average order value

max / min

Return the maximum and minimum values in the group. Both work on numeric, string, and date/time types. Syntax
Arguments
  • col — the expression to reduce.
Returned value
  • The largest or smallest value; the type matches col.
Example
First and last activity timestamps per user

avg_if / count_if / max_if / min_if / sum_if / uniq_if

Conditional aggregates that apply their base aggregate only to the rows where the boolean condition is true. count_if takes just the condition; uniq_if returns an approximate distinct count. Syntax
Arguments
  • col — the expression to aggregate over the matching rows.
  • cond — the boolean condition that selects which rows are included.
Returned value
  • The result of the base aggregate computed over the matching rows only; the type matches the corresponding unconditional aggregate.
Example
Delivered order counts and value per user

median

Returns the median (the 0.5 quantile) of a numeric column. Syntax
Arguments
  • col — the numeric expression to summarize.
Returned value
  • The median, as a floating-point number.
Example
Median order value per user

arg_min / arg_max

arg_max(arg, val) returns the value of arg from the row where val is maximal; arg_min(arg, val) returns it from the row where val is minimal. Syntax
Arguments
  • arg — the expression whose value is returned.
  • val — the expression that is minimized or maximized to select the row.
Returned value
  • The value of arg at the selected row; its type matches arg.
Example
Each user's first and most recent event name

array_agg

Collects input values into an array. The order of elements is not guaranteed unless the input is explicitly ordered. Syntax
Arguments
  • col — the expression whose values are collected.
Returned value
  • An array of the input values.
Example
Collect every event name per user

groupUniqArray

Collects the distinct values of an expression into an array. The order of elements is not guaranteed. Syntax
Arguments
  • col — the expression whose distinct values are collected.
Returned value
  • An array of the distinct input values.
Example
Distinct statuses seen per user

string_agg / string_agg(distinct)

Concatenate non-null string values into a single string separated by a delimiter. The distinct form deduplicates values before concatenating. Syntax
Arguments
  • col — the string expression to concatenate.
  • delimiter — the separator placed between values.
Returned value
  • A single concatenated string.
Example
Comma-separated event names per user

json_agg

Aggregates the input values into a JSON array. Syntax
Arguments
  • col — the expression whose values are collected into the array.
Returned value
  • A JSON array.
Example
Collect event names into a JSON array per user

json_object_agg

Aggregates key/value pairs into a JSON object. Syntax
Arguments
  • key — the expression used as each object key.
  • value — the expression used as each object value.
Returned value
  • A JSON object.
Example
Count of each status as a JSON object per user

uniqExact

Returns the exact number of distinct non-null values, in contrast to the approximate approx_distinct. Syntax
Arguments
  • col — the expression whose distinct values are counted.
Returned value
  • The exact number of distinct values, as an integer.
Example
Exact number of distinct event types per user

percentile_cont / percentile_disc

Ordered-set aggregates that compute a percentile of an ordered column. percentile_cont interpolates a continuous value at the requested fraction; percentile_disc returns the first actual input value whose cumulative position reaches the fraction. Syntax
Arguments
  • fraction — the percentile as a fraction in the range [0, 1].
  • col — the numeric ordering expression.
Returned value
  • percentile_cont returns an interpolated floating-point value; percentile_disc returns an actual input value whose type matches col.
Example
Median and 90th-percentile order value per user

topK

Returns an array of the n approximately most frequent values, in descending order of frequency. It uses the Filtered Space-Saving algorithm and does not guarantee exact results. (ClickHouse only) Syntax
Arguments
  • n — the number of values to return (maximum 65536).
  • col — the expression whose frequent values are found.
Returned value
  • An array of up to n values, ordered from most to least frequent.
Example
Three most frequent events per user

retention

Evaluates a list of conditions per row and returns an array of 0/1 flags where the first element equals cond1, and each later element is 1 only when both cond1 and that condition are met. Summing the arrays element-wise across a cohort produces a retention curve. (ClickHouse only) Syntax
Arguments
  • cond1 ... condN — up to 32 boolean conditions, evaluated per row.
Returned value
  • An array<uint8> of 0/1 flags, one per condition.
Example
Day-0, day-1, and day-7 retention flags per user

windowFunnel

Searches for an ordered event chain within a sliding time window and returns the maximum number of consecutive conditions satisfied, starting from the first. The timestamp must be an unsigned integer, Date, or DateTime; wrap DateTime64 columns with unix_timestamp(). (ClickHouse only) Syntax
Arguments
  • window — the length of the sliding window, in the timestamp’s units.
  • timestamp — the ordering column for the event chain.
  • cond1 ... condN — the ordered event conditions.
Returned value
  • The funnel depth reached, an integer from 0 to N.
Example
Deepest step reached in a view, cart, purchase funnel (7-day window)

quantile / quantileExact / quantileTDigest

Compute the value at a given quantile level of a numeric column. quantile is a fast approximate quantile, quantileExact computes the exact quantile (at higher memory cost), and quantileTDigest uses the t-digest algorithm for an approximate quantile with a strong precision-to-memory ratio. Syntax
Arguments
  • level — the quantile level as a fraction in the range [0, 1] (for example, 0.95).
  • col — the numeric expression to summarize.
Returned value
  • The quantile value, as a floating-point number: approximate for quantile and quantileTDigest, exact for quantileExact.
Example
Approximate, exact, and t-digest 95th percentile per user

approx_distinct

Returns an approximate count of distinct non-null values. Trades exactness for speed and low memory, which makes it well-suited to high-cardinality columns. Syntax
Arguments
  • col — the expression whose distinct values are counted.
Returned value
  • The approximate number of distinct values, as an integer.
Example
Users who triggered many distinct event types

bool_and / bool_or

bool_and returns true when every non-null input is true; bool_or returns true when at least one non-null input is true. Syntax
Arguments
  • col — the boolean expression to reduce.
Returned value
  • A boolean.
Example
Users whose every order was delivered

mode

Returns the most frequently occurring value in the group. Ties are broken by the ordering of the input. Syntax
Arguments
  • col — the expression whose most frequent value is returned.
Returned value
  • The most common value; the type matches col.
Example
Most common event per user

approx_percentile_cont

Returns an approximate, interpolated value at the requested percentile of a numeric column. Syntax
Arguments
  • col — the numeric expression to summarize.
  • level — the percentile as a fraction in the range [0, 1] (for example, 0.9 for the 90th percentile).
Returned value
  • The approximate percentile value, as a floating-point number.
Example
Approximate 90th-percentile order value per user

stddev_pop / stddev_samp

Return the population and sample standard deviation of a numeric column. STDDEV is an alias for stddev_samp. Syntax
Arguments
  • col — the numeric expression to summarize.
Returned value
  • The standard deviation, as a floating-point number.
Example
Spread of order values per user

var_pop / var_samp / variance

Return the population and sample variance of a numeric column. variance is an alias for var_samp. Syntax
Arguments
  • col — the numeric expression to summarize.
Returned value
  • The variance, as a floating-point number.
Example
Variance of order values per user

corr

Returns the Pearson correlation coefficient between two numeric expressions, computed over rows where both are non-null. Syntax
Arguments
  • y — the first numeric expression.
  • x — the second numeric expression.
Returned value
  • The correlation coefficient in the range [-1, 1], as a floating-point number.
Example
Correlation between order amount and item count per user

covar_pop / covar_samp

Return the population and sample covariance between two numeric expressions, over rows where both are non-null. Syntax
Arguments
  • y — the first numeric expression.
  • x — the second numeric expression.
Returned value
  • The covariance, as a floating-point number.
Example
Sample covariance of amount and item count per user

regr_avgx / regr_avgy / regr_count / regr_intercept / regr_r2 / regr_slope / regr_sxx / regr_sxy / regr_syy

Linear-regression aggregates over a dependent variable y and an independent variable x, computed across rows where both are non-null. Syntax
Arguments
  • y — the dependent-variable numeric expression.
  • x — the independent-variable numeric expression.
Returned value
  • regr_count returns the number of non-null (y, x) pairs as an integer. The others return floating-point numbers: regr_avgy and regr_avgx the means of y and x; regr_slope and regr_intercept the slope and y-intercept of the least-squares-fit line; regr_r2 the coefficient of determination; regr_sxx and regr_syy the sums of squares of x and of y, and regr_sxy the sum of products of x and y.
Example
Trend of order amount against item count per user