Skip to content

Aggregation ​

Grouped reads of Atscript DB ($groupBy with aggregate functions) become one OData v4 $apply request, on both hosts, for an entity set the service declares $apply for. The browser client's aggregate(), a Moost server table's aggregate() and Moost DB's /query?$groupBy=… send the same request.

ts
await odata.client(fin.LineItem).aggregate({
  filter: { CompanyCode: "1000" },
  controls: {
    $groupBy: ["CompanyCode", "Currency"],
    $select: ["CompanyCode", "Currency", { $fn: "sum", $field: "Amount", $as: "total" }],
    $sort: { total: -1 },
    $limit: 10,
  },
});
// LineItems?$apply=filter(CompanyCode eq '1000')/groupby((CompanyCode,Currency),
//   aggregate(Amount with sum as total))&$orderby=total desc&$top=10
// → [{ CompanyCode: "1000", Currency: "EUR", total: "1500.25" }, …]

Over HTTP: GET /api/fin/LineItems/query?CompanyCode=1000&$groupBy=CompanyCode,Currency&$select=CompanyCode,Currency,sum(Amount):total&$sort=-total&$limit=10. The query shape is Atscript DB's; see its aggregation reference.

See it live

The charts on the Overview of the browser app are three aggregate() calls of odata.client(roastery.Order) and odata.client(roastery.Coffee), their rows typed by what they group and select. Code: OverviewScreen.vue.

Where it works ​

  • OData v4, on an entity set that declares Aggregation.ApplySupported, on the set, its type or the entity container (RAP writes it on the container).
  • Not OData v2. An analytical set (sap:semantics="aggregate") aggregates by the dimensions and measures of a $select, with no function to choose; it is not supported, and every grouped read of a v2 set is refused.

A set without $apply lists no aggregate functions in its metadata, so Atscript UI offers no grouping there.

What is sent ​

Atscript DB$apply
filterfilter(<filter>) first
$groupBygroupby((<properties>), …)
{ $fn, $field, $as }aggregate(<field> with sum|min|max|average|countdistinct as <alias>)
{ $fn: "count" } without a field$count as <alias>
$havingfilter(<having>) after the grouping
$sort, $skip, $limit$orderby, $skip, $top, applied to the groups
$count$top=0&$count=true: answers [{ count }], the number of groups
  • A related row of a to-one navigation filters the rows as in reads: { order: { $some: { status_code: { $ne: "X" } } } } is filter((order ne null and order/status_code ne 'X')), on both hosts. On a to-many navigation the server refuses it (REL_FILTER_NOT_SUPPORTED); see Filtering by a related row.
  • $groupBy: [] is one row over every matching row.
  • A grouping without aggregates gives the distinct values of the fields (what Atscript UI's distinct-values filter asks for).
  • Rows hold the grouped fields (a member of a complex property nested, as in reads) and every aggregate under its alias, decimals as strings (a sum over a decimal is typed number: see Type Safety).
  • An alias the service could take for a property (RAP refuses those as "already used") is sent under another name and comes back under yours.

Functions ​

An SAP service here is one an SAP S/4HANA® or SAP Gateway® system produced (see Dialects and Vendors).

FunctionSAP serviceOther services
sum, avg, min, maxyesyes
count (rows)noyes
countDistinctnoyes
count of a fieldnono

SAP services answer $count and countdistinct inside $apply with 501, so the hosts do not offer them there. The metadata lists the functions a set offers in aggregateFns.

Refused before any request ​

On both hosts (browser: ClientError 400 NOT_SUPPORTED; server: 400 NOT_SUPPORTED), the error entry's path naming the field or control:

  • a set without $apply, and every set on OData v2;
  • sum or avg of what is no number, and count of a field;
  • an amount or quantity aggregated without its currency or unit in $groupBy;
  • a path through a navigation, a collection or a structured value;
  • $search in a grouped read;
  • calendar buckets, expressions, first / last, conditional aggregates, field lists and two-level aggregates.

The browser client also refuses a plain $select field that is not in $groupBy (INVALID_VALUE).

Left to the service ​

Which properties a set lets you group by or aggregate is the service's knowledge, and so is an amount compared in $having without its currency. The hosts send such a request, and the service's 400 reaches the caller with its message (INVALID_QUERY on the server).

DOs and DON'Ts ​

  • Group an amount by its currency (and a quantity by its unit) — the hosts refuse the sum otherwise.
  • Check aggregateFns of the metadata before offering a function — SAP services have no counts in $apply.
  • Aggregate an OData v2 analytical service another way — it is not supported here.

See also ​

SAP, SAP S/4HANA, SAP Fiori, SAPUI5, SAP BTP and other SAP products and services mentioned herein, as well as their respective logos, are trademarks or registered trademarks of SAP SE (or an SAP affiliate company) in Germany and other countries. Atscript OData is an independent open-source project and is not affiliated with, sponsored or endorsed by SAP SE.
Released under the MIT License.