Developers

Aggregation

Grouped totals with $select, $group and $having.

GET /api/entities/aggregate groups entities and works out totals for each group. $impl and $where choose the entities before grouping, exactly as for a query. The other parameters work on the grouped rows.

GET /api/entities/aggregate?$impl=Payroll&$select=name=employeeName, count=cnt(), most=max(salary)&$group=employeeName&$having=most > 10000&$order=-most
json
[{ "name": "Grace Hopper", "count": 100, "most": 107000 }, { "name": "Alan Turing", "count": 50, "most": 35000 }]

Parameters

ParameterMeaningExample
$selectRequired. Comma-separated alias=expression: a field in $group, or a function. A bare field is short for field=field.$select=name=employeeName, count=cnt(), most=max(salary)
$groupFields to group by. Leave it out to total every matching entity into one row.$group=employeeName
$havingA filter over the aliases, after grouping. Repeat it to AND several.$having=most > 10000
$orderSort keys over the aliases.$order=-most
$skip, $takePage the rows. Without $take, every row is returned.$take=10

Functions

FunctionResultField types
cnt()The number of entities in the group.—
cnt(field)How many of them have a value for the field.Any
sum(field), avg(field)The total and mean of the values present.Integer, Decimal
min(field), max(field)The smallest and largest value present. Text compares ignoring case.Integer, Decimal, DateTime, Text

Rules

  • Without $group the result is always one row (before $having), even when nothing matches: cnt() is 0 and the others null. With $group, nothing matching means [].
  • Missing values in a group field form their own group, whose value is null, sorted last. The functions ignore missing values.
  • Text groups ignore case: Leeds and LEEDS are one group.
  • Types in $having: cnt aliases are Integer, sum and avg Decimal, and min, max and grouped fields have their field's type.
  • Security: only entities the caller can see are counted, and every field in $where, $group and $select must be readable (else 403), so totals can't reveal hidden values.
  • Errors (400, with the parameter and position): a plain field that isn't grouped, an unknown function or field, a function on the wrong type (sum(firstName)), an alias used twice, an alias in $having or $order that isn't selected, metadata fields, and grouping on a Json field.
  • Cost: every matching entity is loaded once. Memory grows with the number of groups, not of entities.

Counting

To count entities, aggregate without a group:

GET /api/entities/aggregate?$impl=Customer&$where=creditLimit > 1000&$select=count=cnt()

Where else aggregates appear

  • Dashboards: an aggregate data source takes the same select, group, having, where and order. See Dashboards.
  • OmniCore Admin: the Aggregates tab.