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
col— the expression to count; omit (use*) to count rows.
- The count, as an integer.
Total and distinct event counts per active user
sum
Returns the sum of the non-null numeric input values. Syntaxcol— the numeric expression to add up.
- The sum; its type follows the input’s numeric type.
High-spend users
avg
Returns the arithmetic mean of the non-null numeric input values. Syntaxcol— the numeric expression to average.
- The mean, as a floating-point number.
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. Syntaxcol— the expression to reduce.
- The largest or smallest value; the type matches
col.
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
col— the expression to aggregate over the matching rows.cond— the boolean condition that selects which rows are included.
- The result of the base aggregate computed over the matching rows only; the type matches the corresponding unconditional aggregate.
Delivered order counts and value per user
median
Returns the median (the 0.5 quantile) of a numeric column. Syntaxcol— the numeric expression to summarize.
- The median, as a floating-point number.
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
arg— the expression whose value is returned.val— the expression that is minimized or maximized to select the row.
- The value of
argat the selected row; its type matchesarg.
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. Syntaxcol— the expression whose values are collected.
- An array of the input values.
Collect every event name per user
groupUniqArray
Collects the distinct values of an expression into an array. The order of elements is not guaranteed. Syntaxcol— the expression whose distinct values are collected.
- An array of the distinct input values.
Distinct statuses seen per user
string_agg / string_agg(distinct)
Concatenate non-null string values into a single string separated by a delimiter. Thedistinct form deduplicates values before concatenating.
Syntax
col— the string expression to concatenate.delimiter— the separator placed between values.
- A single concatenated string.
Comma-separated event names per user
json_agg
Aggregates the input values into a JSON array. Syntaxcol— the expression whose values are collected into the array.
- A JSON array.
Collect event names into a JSON array per user
json_object_agg
Aggregates key/value pairs into a JSON object. Syntaxkey— the expression used as each object key.value— the expression used as each object value.
- A JSON object.
Count of each status as a JSON object per user
uniqExact
Returns the exact number of distinct non-null values, in contrast to the approximateapprox_distinct.
Syntax
col— the expression whose distinct values are counted.
- The exact number of distinct values, as an integer.
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
fraction— the percentile as a fraction in the range[0, 1].col— the numeric ordering expression.
percentile_contreturns an interpolated floating-point value;percentile_discreturns an actual input value whose type matchescol.
Median and 90th-percentile order value per user
topK
Returns an array of then 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
n— the number of values to return (maximum 65536).col— the expression whose frequent values are found.
- An array of up to
nvalues, ordered from most to least frequent.
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 equalscond1, 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
cond1 ... condN— up to 32 boolean conditions, evaluated per row.
- An
array<uint8>of 0/1 flags, one per condition.
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
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.
- The funnel depth reached, an integer from 0 to N.
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
level— the quantile level as a fraction in the range[0, 1](for example,0.95).col— the numeric expression to summarize.
- The quantile value, as a floating-point number: approximate for
quantileandquantileTDigest, exact forquantileExact.
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. Syntaxcol— the expression whose distinct values are counted.
- The approximate number of distinct values, as an integer.
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
col— the boolean expression to reduce.
- A boolean.
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. Syntaxcol— the expression whose most frequent value is returned.
- The most common value; the type matches
col.
Most common event per user
approx_percentile_cont
Returns an approximate, interpolated value at the requested percentile of a numeric column. Syntaxcol— the numeric expression to summarize.level— the percentile as a fraction in the range[0, 1](for example,0.9for the 90th percentile).
- The approximate percentile value, as a floating-point number.
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
col— the numeric expression to summarize.
- The standard deviation, as a floating-point number.
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
col— the numeric expression to summarize.
- The variance, as a floating-point number.
Variance of order values per user
corr
Returns the Pearson correlation coefficient between two numeric expressions, computed over rows where both are non-null. Syntaxy— the first numeric expression.x— the second numeric expression.
- The correlation coefficient in the range
[-1, 1], as a floating-point number.
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. Syntaxy— the first numeric expression.x— the second numeric expression.
- The covariance, as a floating-point number.
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 variabley and an independent variable x, computed across rows where both are non-null.
Syntax
y— the dependent-variable numeric expression.x— the independent-variable numeric expression.
regr_countreturns the number of non-null(y, x)pairs as an integer. The others return floating-point numbers:regr_avgyandregr_avgxthe means ofyandx;regr_slopeandregr_interceptthe slope and y-intercept of the least-squares-fit line;regr_r2the coefficient of determination;regr_sxxandregr_syythe sums of squares ofxand ofy, andregr_sxythe sum of products ofxandy.
Trend of order amount against item count per user