topk_agg(expr: storable, k: const int64) -> array of generic array

Returns an approximation of the top K most frequent values in the input, along with their approximate frequencies.

The output is an array of arrays. In the inner arrays, the first entry is the value in the input, while the second entry is its frequency. The outer array contains k elements, sorted by their frequencies in descending order.

Domain

This is an aggregate function (aggregates rows over a group in aggregate verbs).

This is a window function (calculates over a group of multiple input rows using windowing).

Categories

Examples

statsby top_names:topk_agg(name, 2), group_by(class)

Given the following input:

nameclass
JackA
JoeA
AliceA
AliceA
TomB
JoeB
KathyB
MikeA
TomB

It shall return the following output:

classtop_names
A[["Alice", 2], ["Jack", 1]]
B[["Tom", 2], ["Kathy", 1]]

Note that if there is a tie in the last position the result can be non-deterministic. Any of the values with the same frequency may be included in the last position.