Aggregation functions

Overview

Functions are used when building the aggr parameter of a request for an aggregating selection from a dynamic class collection.

The very fact of building an aggregating request requires using at least one aggregation function.

Filter construction order

The value of the aggr parameter when building an aggregating request is an object whose keys are the returned fields, each containing an aggregated value. The value of these keys must be a list whose first element is one of the aggregation functions and other elements are parameters of that function.

For example:
["count",1]

["sum",["property","pint"]]

Any superposition of supported general-purpose functions can be specified as a parameter.

For example:
["sum",["%",["-",["property","pint"],1],10]]

In the general case, the aggr parameter value may look like this:

.

{
  "x": ["sum",["//",["-",["property","pint"],1],10]],
  "any": ["or",["property","pbool"]]
}

List of supported functions

All functions are applied to values in a group with the same base.

  • count/1 - counts the number of elements in a group. The parameter is an arbitrary value.

  • count_distinct/1 - counts the number of distinct values in a group. Null values are not counted. For example, the number of unique senders by days: aggr={"cnt":["count_distinct",["property","from"]]}&groupby={"day":["toYYYYMMDD",["property","dateTime"]]}.

  • sum/1 - sums a numeric value. The parameter is usually the function ["property",FIELDNAME]. In some cases generating 0 or 1 by condition may be useful.

  • min/1 - computes the minimum value.

  • max/1 - computes the maximum value.

  • avg/1 - computes the average value. To build a weighted sum, a function of the value can be passed as a parameter, for example multiplication by a weight coefficient that is also computed as a function.

  • and/1 - boolean multiplication of values.

  • or/1 - boolean addition of values.

  • join/2 - joins group values into a string via a delimiter. The first argument is the delimiter (constant or function), the second is the value (arbitrary function), for example ["join",", ",["property","name"]]. Argument order is the same as for the general-purpose join function.