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[{ "name": "Grace Hopper", "count": 100, "most": 107000 }, { "name": "Alan Turing", "count": 50, "most": 35000 }]Parameters
| Parameter | Meaning | Example |
|---|---|---|
$select | Required. 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) |
$group | Fields to group by. Leave it out to total every matching entity into one row. | $group=employeeName |
$having | A filter over the aliases, after grouping. Repeat it to AND several. | $having=most > 10000 |
$order | Sort keys over the aliases. | $order=-most |
$skip, $take | Page the rows. Without $take, every row is returned. | $take=10 |
Functions
| Function | Result | Field 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
$groupthe result is always one row (before$having), even when nothing matches:cnt()is0and the othersnull. 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:
LeedsandLEEDSare one group. - Types in
$having:cntaliases are Integer,sumandavgDecimal, andmin,maxand grouped fields have their field's type. - Security: only entities the caller can see are counted, and every field in
$where,$groupand$selectmust 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$havingor$orderthat 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,whereandorder. See Dashboards. - OmniCore Admin: the Aggregates tab.