74
OData Extension for Data Aggregation Version 4.0 Working Draft 04 05 16 October 20 08 Dec ember February 2017 6 5 Technical Committee: OASIS Open Data Protocol (OData) TC Chairs: Ralf Handl ([email protected]), SAP SE Ram Jeyaraman ([email protected]), Microsoft Editor: Ralf Handl ([email protected]), SAP SE Hubert Heijkers ([email protected]), IBM Gerald Krause ([email protected]), SAP SE Michael Pizzo ([email protected]), Microsoft Martin Zurmuehl ([email protected]), SAP SE Heiko Theißen ( [email protected] ), SAP SE Additional artifacts: This prose specification is one component of a Work Product that consists of: OData Extension for Data Aggregation Version 4.0 (this document) OData Aggregation ABNF Construction Rules Version 4.0 OData Aggregation ABNF Test Cases OData Aggregation Vocabulary Related work: This specification is related to: OData Version 4.0 Part 1: Protocol OData Version 4.0 Part 2: URL Conventions OData Version 4.0 Part 3: CSDL OData ABNF Construction Rules Version 4.0 OData ABNF Test Cases OData Core Vocabulary OData Measures Vocabulary OData JSON Format Version 4.0 This specification replaces or supersedes: None Declared XML namespaces: None Abstract: This specification adds basic grouping and aggregation functionality (e.g. sum, min, and max) to the Open Data Protocol (OData) without changing any of the base principles of OData. odata-data-aggregation-ext-v4.0-wd05 Working Draft 05 21 June 2016 Standards Track Draft Copyright © OASIS Open 2016. All Rights Reserved. Page 1 of 74

 · Web viewOpen Data (OData) services expose a data model that describes the schema of the service in terms of the Entity Data Model (EDM, see ) and then allows for querying data

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1:  · Web viewOpen Data (OData) services expose a data model that describes the schema of the service in terms of the Entity Data Model (EDM, see ) and then allows for querying data

OData Extension for Data Aggregation Version 4.0

Working Draft 0405

16 October2008 DecemberFebruary 201765

Technical Committee:OASIS Open Data Protocol (OData) TC

Chairs:Ralf Handl ([email protected]), SAP SERam Jeyaraman ([email protected]), Microsoft

Editor:Ralf Handl ([email protected]), SAP SEHubert Heijkers ([email protected]), IBMGerald Krause ([email protected]), SAP SEMichael Pizzo ([email protected]), MicrosoftMartin Zurmuehl ([email protected]), SAP SEHeiko Theißen ([email protected]), SAP SE

Additional artifacts:This prose specification is one component of a Work Product that consists of: OData Extension for Data Aggregation Version 4.0 (this document) OData Aggregation ABNF Construction Rules Version 4.0 OData Aggregation ABNF Test Cases OData Aggregation Vocabulary

Related work:This specification is related to: OData Version 4.0 Part 1: Protocol OData Version 4.0 Part 2: URL Conventions OData Version 4.0 Part 3: CSDL OData ABNF Construction Rules Version 4.0 OData ABNF Test Cases OData Core Vocabulary OData Measures Vocabulary OData JSON Format Version 4.0This specification replaces or supersedes: None

Declared XML namespaces: None

Abstract:This specification adds basic grouping and aggregation functionality (e.g. sum, min, and max) to the Open Data Protocol (OData) without changing any of the base principles of OData.

Status:This Working Draft (WD) has been produced by one or more TC Members; it has not yet been voted on by the TC or approved as a Committee Draft (Committee Specification Draft or a Committee Note Draft). The OASIS document Approval Process begins officially with a TC vote to approve a WD as a Committee Draft. A TC may approve a Working Draft, revise it, and re-approve it any number of times as a Committee Draft.

Copyright © OASIS Open 2016. All Rights Reserved.

odata-data-aggregation-ext-v4.0-wd05 Working Draft 05 21 June 2016Standards Track Draft Copyright © OASIS Open 2016. All Rights Reserved. Page 1 of 61

Handl, Ralf, 09/08/17,
Reference V4.01?
Krause, Gerald, 05/29/20,
Applying some of the issues introduced references to 4.01, so I’d say Yes.
Page 2:  · Web viewOpen Data (OData) services expose a data model that describes the schema of the service in terms of the Entity Data Model (EDM, see ) and then allows for querying data

All capitalized terms in the following text have the meanings assigned to them in the OASIS Intellectual Property Rights Policy (the "OASIS IPR Policy"). The full Policy may be found at the OASIS website.This document and translations of it may be copied and furnished to others, and derivative works that comment on or otherwise explain it or assist in its implementation may be prepared, copied, published, and distributed, in whole or in part, without restriction of any kind, provided that the above copyright notice and this section are included on all such copies and derivative works. However, this document itself may not be modified in any way, including by removing the copyright notice or references to OASIS, except as needed for the purpose of developing any document or deliverable produced by an OASIS Technical Committee (in which case the rules applicable to copyrights, as set forth in the OASIS IPR Policy, must be followed) or as required to translate it into languages other than English.The limited permissions granted above are perpetual and will not be revoked by OASIS or its successors or assigns.This document and the information contained herein is provided on an "AS IS" basis and OASIS DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTY THAT THE USE OF THE INFORMATION HEREIN WILL NOT INFRINGE ANY OWNERSHIP RIGHTS OR ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.

odata-data-aggregation-ext-v4.0-wd05 Working Draft 05 21 June 2016Standards Track Draft Copyright © OASIS Open 2016. All Rights Reserved. Page 2 of 61

Page 3:  · Web viewOpen Data (OData) services expose a data model that describes the schema of the service in terms of the Entity Data Model (EDM, see ) and then allows for querying data

Table of Contents1 Introduction......................................................................................................................................... 5

1.1 Terminology....................................................................................................................................... 51.2 Normative References....................................................................................................................... 51.3 Non-Normative References...............................................................................................................51.4 Typographical Conventions...............................................................................................................5

2 Overview............................................................................................................................................. 62.1 Definitions.......................................................................................................................................... 62.2 Example Data Model......................................................................................................................... 72.3 Example Data.................................................................................................................................... 82.4 Example Use Cases.......................................................................................................................... 8

3 System Query Option $apply..........................................................................................................103.1 Transformation aggregate............................................................................................................11

3.1.1 Keyword as.............................................................................................................................. 113.1.2 Keyword with.......................................................................................................................... 123.1.3 Aggregation Methods...............................................................................................................12

3.1.3.1 Standard Aggregation Method sum.....................................................................................................123.1.3.2 Standard Aggregation Method min.................................................................................................12133.1.3.3 Standard Aggregation Method max.....................................................................................................133.1.3.4 Standard Aggregation Method average............................................................................................133.1.3.5 Standard Aggregation Method countdistinct...........................................................................13143.1.3.6 Custom Aggregation Methods............................................................................................................14

3.1.4 Keyword from.......................................................................................................................... 143.1.5 Virtual Property $count...........................................................................................................15

3.2 Transformation topcount..............................................................................................................163.3 Transformation topsum.................................................................................................................. 163.4 Transformation toppercent..........................................................................................................173.5 Transformation bottomcount........................................................................................................173.6 Transformation bottomsum............................................................................................................183.7 Transformation bottompercent...................................................................................................183.8 Transformation identity..............................................................................................................193.9 Transformation concat.................................................................................................................. 193.10 Transformation groupby..............................................................................................................20

3.10.1 Simple Grouping....................................................................................................................203.10.2 Grouping with rollup and $all...........................................................................................21

3.11 Transformation filter................................................................................................................233.12 Transformation expand............................................................................................................25233.13 Transformation search............................................................................................................25243.14 Transformation compute..........................................................................................................25243.15 Filter Function isdefined.......................................................................................................27263.16 Evaluating $apply.................................................................................................................... 28273.17 Evaluating $apply as an Expand Option.................................................................................29273.18 ABNF for Extended URL Conventions.......................................................................................2927

odata-data-aggregation-ext-v4.0-wd05 Working Draft 05 21 June 2016Standards Track Draft Copyright © OASIS Open 2016. All Rights Reserved. Page 3 of 61

Page 4:  · Web viewOpen Data (OData) services expose a data model that describes the schema of the service in terms of the Entity Data Model (EDM, see ) and then allows for querying data

4 Representation of Aggregated Instances......................................................................................30285 Cross-Joins and Aggregation........................................................................................................31296 Vocabulary for Data Aggregation..................................................................................................3230

6.1 Aggregation Capabilities..............................................................................................................32306.2 Property Annotations................................................................................................................... 3230

6.2.1 Groupable Properties...........................................................................................................32306.2.2 Aggregatable Properties.......................................................................................................32306.2.3 Custom Aggregates.............................................................................................................33316.2.4 Context-Defining Properties.................................................................................................34326.2.5 Annotation Example.............................................................................................................3432

6.3 Hierarchies.................................................................................................................................. 35336.3.1 Leveled Hierarchy................................................................................................................35336.3.2 Recursive Hierarchy.............................................................................................................3533

6.3.2.1 Hierarchy Filter Functions...............................................................................................................35336.3.3 Hierarchy Examples.............................................................................................................3634

6.4 Actions and Functions on Aggregated Entities............................................................................38367 Examples...................................................................................................................................... 4038

7.1 Requesting Distinct Values..........................................................................................................40387.2 Standard Aggregation Methods...................................................................................................41397.3 Requesting Custom Aggregates..................................................................................................44427.4 Aliasing........................................................................................................................................ 45437.5 Combining Transformations per Group.......................................................................................46447.6 Model Functions as Set Transformations....................................................................................48457.7 Controlling Aggregation per Rollup Level....................................................................................48467.8 Transformation Sequences.........................................................................................................4947

8 Conformance................................................................................................................................. 5250Appendix A.......................................................................................................................Acknowledgments

5351Appendix B..........................................................................................................................Revision History

5452

odata-data-aggregation-ext-v4.0-wd05 Working Draft 05 21 June 2016Standards Track Draft Copyright © OASIS Open 2016. All Rights Reserved. Page 4 of 61

Page 5:  · Web viewOpen Data (OData) services expose a data model that describes the schema of the service in terms of the Entity Data Model (EDM, see ) and then allows for querying data

1 IntroductionThis specification adds the notion of aggregation to the Open Data Protocol (OData) without changing any of the base principles of OData. It defines semantics and a representation for aggregation of data, especially:

Semantics and operations for querying aggregated data, Results format for queries containing aggregated data, Vocabulary terms to annotate what can be aggregated, and how.

1.1 TerminologyThe key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in [RFC2119].

1.2 Normative References[OData-ABNF] OData ABNF Construction Rules Version 4.0.

See the link in "Related work" section on cover page.[OData-Agg-ABNF] OData Aggregation ABNF Construction Rules Version 4.0.

See link in "Additional artifacts" section on cover page. [OData-CSDL] OData Version 4.0 Part 3: CSDL.

See link in "Related work" section on cover page.[OData-JSON] OData JSON Format Version 4.0.

See link in "Related work" section on cover page.[OData-Protocol] OData Version 4.0 Part 1: Protocol.

See link in "Related work" section on cover page.[OData-URL] OData Version 4.0 Part 2: URL Conventions.

See link in "Related work" section on cover page.[OData-VocAggr] OData Aggregation Vocabulary.

See link in "Additional artifacts" section on cover page.[OData-VocMeas] OData Measures Vocabulary.

See link in "Related work" section on cover page.[RFC2119] Bradner, S., “Key words for use in RFCs to Indicate Requirement Levels”, BCP

14, RFC 2119, March 1997. http://www.ietf.org/rfc/rfc2119.txt.

1.3 Non-Normative References[TSQL ROLLUP] http://msdn.microsoft.com/en-us/library/bb522495.aspx

1.4 Typographical ConventionsKeywords defined by this specification use this monospaced font.

Normative source code uses this paragraph style.

Some sections of this specification are illustrated with non-normative examples.

Example 1: text describing an example uses this paragraph style

Non-normative examples use this paragraph style.

All examples in this document are non-normative and informative only.All other text is normative unless otherwise labeled.

odata-data-aggregation-ext-v4.0-wd05 Working Draft 05 21 June 2016Standards Track Draft Copyright © OASIS Open 2016. All Rights Reserved. Page 5 of 61

Krause, Gerald, 05/27/20,
Applying some of the issues introduced references to 4.01, so I’d say Yes.
Handl, Ralf, 09/08/17,
Reference V4.01?
Page 6:  · Web viewOpen Data (OData) services expose a data model that describes the schema of the service in terms of the Entity Data Model (EDM, see ) and then allows for querying data

2 OverviewOpen Data (OData) services expose a data model that describes the schema of the service in terms of the Entity Data Model (EDM, see [OData-CSDL]) and then allows for querying data in terms of this model. The responses returned by an OData service are based on that data model and retain the relationships between the entities in the model. Extending the OData query features with simple aggregation capabilities avoids cluttering OData services with an exponential number of explicitly modeled “aggregation level entities” or else restricting the consumer to a small subset of predefined aggregations. Adding the notion of aggregation to OData without changing any of the base principles in OData has two aspects:

1. Means for the consumer to query aggregated data on top of any given data model (for sufficiently capable data providers)

2. Means for the provider to annotate what data can be aggregated, and in which way, allowing consumers to avoid asking questions that the provider cannot answer.

Implementing any of these two aspects is valuable in itself independent of the other, and implementing both provides additional value for consumers. The descriptions provided by the provider help a consumer understand more of the data structure looking at the service's exposed data model. The query extensions allow the consumers to express explicitly the desired aggregation behavior for a particular query. They also allow consumers to formulate queries that refer to the annotations as shorthand.

2.1 DefinitionsThis specification defines the following terms:

Aggregatable Property – a property for which the values can be aggregated using an aggregation method

Aggregation Method – a method that can be used to aggregate an aggregatable property or expression

Standard Aggregation Method – one of the standard aggregation methods: sum, min, max, average, and countdistinct

Custom Aggregation Method – a custom aggregation method that can be applied to expressions of a specified type

Custom Aggregate – a dynamic property that can appear in an aggregate clause Groupable Property – a property whose values can be used to group entities or complex type

instances for aggregation Hierarchy – an arrangement of groupable properties whose values are represented as being

“above”, “below”, or “at the same level as” one another Expression – derived from the commonExpr rule (see [OData-ABNF]) Aggregatable Primitive Type – a primitive type other than Edm.Stream or subtypes of

Edm.Geography or Edm.Geometry

Aggregatable Expression – an expression resulting in a value of an aggregatable primitive type

odata-data-aggregation-ext-v4.0-wd05 Working Draft 05 21 June 2016Standards Track Draft Copyright © OASIS Open 2016. All Rights Reserved. Page 6 of 61

Krause, Gerald, 05/20/20,
ODATA-1040
Page 7:  · Web viewOpen Data (OData) services expose a data model that describes the schema of the service in terms of the Entity Data Model (EDM, see ) and then allows for querying data

2.2 Example Data ModelExample 2: The following diagram shows the terms defined in the section above applied to a simple model that is used throughout this document.

odata-data-aggregation-ext-v4.0-wd05 Working Draft 05 21 June 2016Standards Track Draft Copyright © OASIS Open 2016. All Rights Reserved. Page 7 of 61

Page 8:  · Web viewOpen Data (OData) services expose a data model that describes the schema of the service in terms of the Entity Data Model (EDM, see ) and then allows for querying data

Key : Type

Property : Type

Navigation Property

Legend:

ID : String

Name : String

Country : String

Customer

Date : Date

Month : String

Quarter : String

Time

Year : Int16

ID : String

Name : String

Category

ID : String

Name : String

Category

Product

Color : String

ID: String

Customer

Time

Sales

Product

Amount : Decimal

SalesOrganization

Code : String

Name : String

Currency

ID : String

Name : String

Superordinate

SalesOrganizationCurrency

TaxRate : Decimal

SalesSales

Products

odata-data-aggregation-ext-v4.0-wd05 Working Draft 05 21 June 2016Standards Track Draft Copyright © OASIS Open 2016. All Rights Reserved. Page 8 of 61

Krause, Gerald, 17.09.18,
ODATA-1162
Page 9:  · Web viewOpen Data (OData) services expose a data model that describes the schema of the service in terms of the Entity Data Model (EDM, see ) and then allows for querying data

odata-data-aggregation-ext-v4.0-wd05 Working Draft 05 21 June 2016Standards Track Draft Copyright © OASIS Open 2016. All Rights Reserved. Page 9 of 61

Page 10:  · Web viewOpen Data (OData) services expose a data model that describes the schema of the service in terms of the Entity Data Model (EDM, see ) and then allows for querying data

Key : Type

Property : Type

Navigation Property

Legend:

ID : String

Name : String

Country : String

Customer

Date : Date

Month : String

Quarter : String

Time

Year : Number

ID : String

Name : String

Category

ID : String

Name : String

Category

Product

Color : String

ID: String

Customer

Time

Sales

Product

Amount : Decimal

SalesOrganization

Code : String

Name : String

Currency

ID : String

Name : String

Superordinate

SalesOrganizationCurrency

TaxRate : String

SalesSales

Products

The Amount property in the Sales entity type is an aggregatable property, and the properties of the related entity types are groupable. These can be arranged in four hierarchies:

Product hierarchy based on groupable properties of the Category and Product entity types

Customer hierarchy based on Country and Customer

Time hierarchy based on Year, Month and Date

SalesOrganization based on the recursive association to itself

In the context of Online Analytical Processing (OLAP), this model might be described in terms of a Sales “cube” with an Amount “measure” and three “dimensions”. This document will avoid such terms, as they are heavily overloaded.

Query extensions and descriptive annotations can both be applied to normalized as well as partly or fully denormalized schemas.

odata-data-aggregation-ext-v4.0-wd05 Working Draft 05 21 June 2016Standards Track Draft Copyright © OASIS Open 2016. All Rights Reserved. Page 10 of 61

Page 11:  · Web viewOpen Data (OData) services expose a data model that describes the schema of the service in terms of the Entity Data Model (EDM, see ) and then allows for querying data

Note that OData’s Entity Data Model (EDM) does not mandate a single storage model; it may be realized as a completely conceptual model whose data structure is calculated on-the-fly for each request. The actual "entity-relationship structure" of the model should be chosen to simplify understanding and querying data for the target audience of a service. Different target audiences may well require differently structured services on top of the same storage model.

odata-data-aggregation-ext-v4.0-wd05 Working Draft 05 21 June 2016Standards Track Draft Copyright © OASIS Open 2016. All Rights Reserved. Page 11 of 61

Krause, Gerald, 20.05.20,
Krause, Gerald, 20.05.20,
ODATA-1162
Page 12:  · Web viewOpen Data (OData) services expose a data model that describes the schema of the service in terms of the Entity Data Model (EDM, see ) and then allows for querying data

2.3 Example DataExample 3: The following sample data will be used to further illustrate the capabilities introduced by this extension.

2.4 Example Use CasesExample 4: In the example model, one prominent use case is the relation of customers to products. The first question that is likely to be asked is: “Which customers bought which products?”

This leads to the second more quantitative question: “Who bought how much of what?”

The answer to the second question typically is visualized as a cross-table:

Food Non-FoodSugar Coffee Paper

USA USD 14 2 12 5 5 Joe USD 6 2 4 1 1 Sue USD 8 8 4 4

Netherlands EUR 2 2 3 3 Sue EUR 2 2 3 3

The data in this cross-table can be written down in a shape that more closely resembles the structure of the data model, leaving cells empty that have been aggregated away:

Customer/Country Customer/Name Product/Category/Name Product/Name Amount Currency /Code

odata-data-aggregation-ext-v4.0-wd05 Working Draft 05 21 June 2016Standards Track Draft Copyright © OASIS Open 2016. All Rights Reserved. Page 12 of 61

Page 13:  · Web viewOpen Data (OData) services expose a data model that describes the schema of the service in terms of the Entity Data Model (EDM, see ) and then allows for querying data

USA Joe Non-Food Paper 1 USDUSA Joe Food Sugar 2 USDUSA Joe Food Coffee 4 USDUSA Sue Food Coffee 8 USDUSA Sue Non-Food Paper 4 USDNetherlands Sue Food Sugar 2 EURNetherlands Sue Non-Food Paper 3 EURUSA Food Sugar 2 USDUSA Food Coffee 12 USDUSA Non-Food Paper 5 USDNetherlands Food Sugar 2 EURNetherlands Non-Food Paper 1 EURUSA Joe Food 6 USDUSA Joe Non-Food 1 USDUSA Sue Food 8 USDUSA Sue Non-Food 4 USDNetherlands Sue Food 2 EURNetherlands Sue Non-Food 3 EURUSA Food 14 USDUSA Non-Food 5 USDNetherlands Food 2 EURNetherlands Non-Food 3 EUR

Note that this result contains seven fully qualified aggregate values, plus fifteen rollup rows with subtotal values, shown in bold.

odata-data-aggregation-ext-v4.0-wd05 Working Draft 05 21 June 2016Standards Track Draft Copyright © OASIS Open 2016. All Rights Reserved. Page 13 of 61

Page 14:  · Web viewOpen Data (OData) services expose a data model that describes the schema of the service in terms of the Entity Data Model (EDM, see ) and then allows for querying data

3 System Query Option $applyAggregation behavior on a collection of instances is triggered using the query option $apply. It takes a sequence of set transformations, separated by forward slashes to express that they are consecutively applied, i.e. the result of each transformation is the input to the next transformation. This is consistent with the use of service-defined bindable and composable functions in path segments.Unless otherwise noted, each set transformation:

preserves the structure of the input type, so the structure of the result fits into the data model of the service.

does not necessarily preserve the number of instances in the result, as this will typically differ from the number of instances in the input set.

does not necessarily guarantee that all properties of the result instances have a well-defined value.

So the actual (or relevant) structure of each intermediary result will often resemble a projection of the original data model that could also have been formed using the standard system query options $expand and $select defined in [OData-Protocol], with dynamic properties representing the aggregate values. The parameters of set transformations allow specifying how the result instances are constructed from the input instances. This especially means that all property paths relate to the structure of the immediate input set.The set transformations defined by this extension are

aggregate topcount topsum toppercent bottomcount bottomsum bottompercent identity concat groupby filter expand search compute

Service-defined bound functions that take an entity set a collection as their binding parameter MAY be used as set transformations within $apply if the type of the binding parameter matches the type of the result set of the preceding transformation. If it returns an entity seta collection, further transformations can follow the bound function. The parameter syntax for bound function segments is identical to the parameter syntax for bound functions in resource path segments or $filter expressions. See section 7.6 for an example.If a data service that supports $apply does not support it on the collection identified by the request resource path, it MUST fail with 501 Not Implemented and a meaningful human-readable error message.The representation of the aggregated result set as response payload is described in section 4. The aggregated entities returned in the request examples in the following sections are transient and represented in the OData 4.01 JSON format that allows to omit the id control information for transient entities. For further brevity, the examples also leave out the type control information required to disambiguate the type of represented dynamic property values.

odata-data-aggregation-ext-v4.0-wd05 Working Draft 05 21 June 2016Standards Track Draft Copyright © OASIS Open 2016. All Rights Reserved. Page 14 of 61

Krause, Gerald, 05/27/20,
ODATA-1137
Krause, Gerald, 05/27/20,
ODATA-1279, the deletions of the 120 occurrences of "@odata.id": null are not commented with this issue :-)
Krause, Gerald, 05/15/20,
ODATA-967
Krause, Gerald, 05/15/20,
ODATA-967Change already reflected in ABNF
Krause, Gerald, 09/17/18,
ODATA-944
Krause, Gerald, 05/20/20,
Or usually, typically, mostly... but not always - transformation nest being an exception, cf. ODATA-1169
Krause, Gerald, 09/17/18,
ODATA-946
Page 15:  · Web viewOpen Data (OData) services expose a data model that describes the schema of the service in terms of the Entity Data Model (EDM, see ) and then allows for querying data

3.1 Transformation aggregate The aggregate transformation takes a comma-separated list of one or more aggregate expressions as parameters and returns a result set with a single instance, representing the aggregated value for all instances in the input set.An aggregate expression may be:

an expression aggregatable expression valid in a $filter system query option on the input set that results in a simple value, e.g. the path to an aggregatable property, with a specified aggregation method,

an aggregatable property that can be reached via a path consisting of a sequence of navigation properties, complex properties and complex collection properties, with a specified aggregation method,

a custom aggregate that can be reached via a path consisting of a sequence of navigation properties, complex properties and complex collection properties,

any of the above, followed by a from expression, any of the above, enclosed in parentheses and prefixed with a navigation path to related entities,

which serve as input set for the enclosed aggregate expression, the virtual property $count., which is a shortcut for the expression 1 with sum

Any aggregate expression that specifies an aggregation method MUST define an alias for the resulting aggregated value. The resulting instance contains one dynamic property per parameter representing the aggregated value across all instances within the input set. The JSON representation of these dynamic properties will include odata.type annotations where required by [OData-JSON]. If paths are present, the corresponding navigation properties are implicitly expanded to make the properties part of the result representation.The aggregate transformation affects the structure of the result set: An aggregatable expression resulting in a simple value and a custom aggregate corresponds to a dynamic property in a $select option. If the expression operates on related entitiesthey are preceded by a navigation path, the corresponding $select option would be nested in one $expand option for each navigation property in the navigation path.

3.1.1 Keyword as Aggregate expressions can define an alias using the as keyword, followed by a SimpleIdentifier (see [OData-CSDL, section 17.2]).The alias will introduce a dynamic property in the aggregated result set. The introduced dynamic property is added to the type containing the original expression or custom aggregate. The alias MUST NOT collide with names of declared properties, custom aggregates, or other aliases in that type.When an aggregation method is specified, an alias MUST be applied to the expression.

Example 5:

GET ~/Sales?$apply=aggregate(Amount with sum as Total,Amount with max as MxA)

results in

{ "@odata.context": "$metadata#Sales(Total,MxA)", "value": [ { "@odata.id": null, "Total": 24, "MxA": 8, ... } ]}

Example 6:

GET ~/Sales?$apply=aggregate(Amount mul Product/TaxRate with sum as Tax)

odata-data-aggregation-ext-v4.0-wd05 Working Draft 05 21 June 2016Standards Track Draft Copyright © OASIS Open 2016. All Rights Reserved. Page 15 of 61

Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 06/02/20,
ODATA-945
Krause, Gerald, 09/17/18,
ODATA-1038
Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 06/02/20,
ODATA-1255
Krause, Gerald, 06/02/20,
ODATA-945
Krause, Gerald, 05/26/20,
ODATA-1256
Krause, Gerald, 09/18/18,
ODATA-1206Change reflected in ABNF test cases
Krause, Gerald, 06/08/20,
ODATA-1206
Krause, Gerald, 09/17/18,
ODATA-1038
Krause, Gerald, 05/25/20,
ODATA-1205Change already reflected in ABNF
Page 16:  · Web viewOpen Data (OData) services expose a data model that describes the schema of the service in terms of the Entity Data Model (EDM, see ) and then allows for querying data

results in

{ "@odata.context": "$metadata#Sales(Tax)", "value": [ { "@odata.id": null, "Tax": 2.08 } ]}

If the aggregate expression is to be evaluated on related entities, the expression and its alias MUST be enclosed in parentheses and prefixed with the navigation path to the related entities. The expression within the parentheses MUST be an expression that could also be used in a $filter system query option on the related entities identified by the navigation path. This syntax is intentionally similar to the syntax of $expand with nested query options.

Example 7:

GET ~/Products?$apply=aggregate(Sales(Amount mul Product/TaxRate with sum as Tax))

results in

{ "@odata.context": "$metadata#Products(Sales(Tax))", "value": [ { "@odata.id": null, "Sales": [ { "Tax": 2.08 } ] } ]}

An alias affects the structure of the result set: each alias corresponds to a dynamic property in a $select option. If the aggregate expression with the alias operates on related entities, this dynamic property that is nested in an $expand option for each navigation property in the path to the related entitiesof the aliased expression.

3.1.2 Keyword with The keyword with is used to apply an aggregation method to an aggregatable expression. The property or expression being aggregated is followed by the keyword with, followed by the name of the aggregation method to apply, followed by the keyword as and an alias.

3.1.3 Aggregation MethodsValues can be aggregated using the standard aggregation methods sum, min, max, average, and countdistinct, or with custom aggregation methods defined by the service. Aggregate expressions containing an aggregation method MUST define an alias for the resulting aggregate value.

3.1.3.1 Standard Aggregation Method sumThe standard aggregation method sum can be applied to numeric values to return the sum of the non-null values, or null if there are no non-null values or the input set is empty. The provider MUST choose a single type for the property across all instances of that type in the result that is capable of representing the aggregated values. This may require a larger integer type, Edm.Decimal with sufficient Precision and Scale, or Edm.Double.

Example 8:

GET ~/Sales?$apply=aggregate(Amount with sum as Total)

results in

{

odata-data-aggregation-ext-v4.0-wd05 Working Draft 05 21 June 2016Standards Track Draft Copyright © OASIS Open 2016. All Rights Reserved. Page 16 of 61

Krause, Gerald, 09/17/18,
ODATA-1040
Krause, Gerald, 06/02/20,
ODATA-945
Krause, Gerald, 06/02/20,
ODATA-945
Krause, Gerald, 06/02/20,
ODATA-945
Krause, Gerald, 09/17/18,
ODATA-1040
Page 17:  · Web viewOpen Data (OData) services expose a data model that describes the schema of the service in terms of the Entity Data Model (EDM, see ) and then allows for querying data

"@odata.context": "$metadata#Sales(Total)", "value": [ { "@odata.id": null, "Total": 24, ... } ]}

3.1.3.2 Standard Aggregation Method minThe standard aggregation method min can be applied to values with a totally ordered domain to return the smallest of the non-null values, or null if there are no non-null values or the input set is empty.The result property will have the same type as the input property.

Example 9:

GET ~/Sales?$apply=aggregate(Amount with min as MinAmount)

results in

{ "@odata.context": "$metadata#Sales(MinAmount)", "value": [ { "@odata.id": null, "MinAmount": 1, ... } ]}

3.1.3.3 Standard Aggregation Method maxThe standard aggregation method max can be applied to values with a totally ordered domain to return the largest of the non-null values, or null if there are no non-null values or the input set is empty.The result property will have the same type as the input property

Example 10:

GET ~/Sales?$apply=aggregate(Amount with max as MaxAmount)

results in

{ "@odata.context": "$metadata#Sales(MaxAmount)", "value": [ { "@odata.id": null, "MaxAmount": 8, ... } ]}

3.1.3.4 Standard Aggregation Method averageThe standard aggregation method average can be applied to numeric values to return the sum of the non-null values divided by the count of the non-null values, or null if there are no non-null values or the input set is empty.The provider MUST choose a single type for the property across all instances of that type in the result that is capable of representing the aggregated values; either Edm.Double or Edm.Decimal with sufficient Precision and Scale.

Example 11:

GET ~/Sales?$apply=aggregate(Amount with average as AverageAmount)

results in

{ "@odata.context": "$metadata#Sales(AverageAmount)",

odata-data-aggregation-ext-v4.0-wd05 Working Draft 05 21 June 2016Standards Track Draft Copyright © OASIS Open 2016. All Rights Reserved. Page 17 of 61

Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 05/19/20,
ODATA-1137
Page 18:  · Web viewOpen Data (OData) services expose a data model that describes the schema of the service in terms of the Entity Data Model (EDM, see ) and then allows for querying data

"value": [ { "@odata.id": null, "AverageAmount": 3.0 } ]}

3.1.3.5 Standard Aggregation Method countdistinctThe aggregation method countdistinct counts the distinct values, omitting any null values. For navigation properties, it counts the distinct entities in the union of all entities related to entities in the input set. For collection-valued primitive properties, it counts the distinct items in the union of all collection values in the input set. The result property MUST have type Edm.Decimal with Scale="0" and sufficient Precision.

Example 12:

GET ~/Sales?$apply=aggregate(Product with countdistinct as DistinctProducts)

results in

{ "@odata.context": "$metadata#Sales(DistinctProducts)", "value": [ { "@odata.id": null, "DistinctProducts": 3, ... } ]}

The number of instances in the input set can be counted with the virtual propertyaggregate expression $count.

3.1.3.6 Custom Aggregation MethodsServices can define custom aggregation methods if the functionality offered by the standard aggregation methods is not sufficient for the intended consumers. Custom aggregation methods MUST use a namespace-qualified name (see [OData-ABNF]), i.e. contain at least one dot. Dot-less names are reserved for future versions of this specification.

Example 13: custom aggregation methods that concatenates distinct string values separated by commas

GET ~/Sales?$apply=groupby((Customer/Country), aggregate(Amount with sum as Total, Product/Name with Custom.concat as ProductNames))

results in

{ "@odata.context": "$metadata#Sales(Customer(Country),AmountTotal,ProductNames)", "value": [ { "@odata.id":null, "Customer": { "Country": "Netherlands" }, "AmountTotal": 5, ProductNames: "Paper,Sugar", ... }, { "@odata.id":null, "Customer": { "Country": "USA" }, "AmountTotal": 19, ProductNames: "Coffee,Paper,Sugar", ... } ]}

3.1.4 Keyword from The from keyword gives control over the order of aggregation across properties that are not part of the result structure and over the aggregation methods applied in every step. Instead of applying a single aggregation method for calculating the aggregated value of an aggregate expression across all properties not included in the result structure, other aggregation methods to be odata-data-aggregation-ext-v4.0-wd05 Working Draft 05 21 June 2016Standards Track Draft Copyright © OASIS Open 2016. All Rights Reserved. Page 18 of 61

Krause, Gerald, 09/17/18,
ODATA-1040
Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 06/02/20,
ODATA-1255
Krause, Gerald, 05/19/20,
ODATA-1137
Page 19:  · Web viewOpen Data (OData) services expose a data model that describes the schema of the service in terms of the Entity Data Model (EDM, see ) and then allows for querying data

applied when aggregating away certain properties MAY be specified using the from keyword, followed by a property path of a groupable property. Each groupable property MUST be followed by a with clause unless the aggregate expression is a custom aggregate, in which case the provider-defined behavior of the custom aggregate is used: aggregateExpression as

from groupableProperty1 [ with aggregationMethod1 ]…

from groupablePropertyn [ with aggregationMet h odn ]

as

If the from keyword is used, an alias MUST be introduced.

If the from keyword is present, first the aggregation method determined by the aggregate expression is used to aggregate away properties that are not mentioned in a from clause and are not grouping properties.Then consecutively properties not part of the result are aggregated away in the order of the from clauses and using the method specified by the from clause.

More formally, the calculation of aggregate with the from keyword is equivalent with a list of set transformations:

groupby((groupableProperty1, …, groupablePropertyn),

aggregate(aggregateExpression as tmpalias1))/groupby((groupableProperty2, …, groupablePropertyn),

aggregate(tmpalias1 with aggregationMethod1 as tmpalias2))…

/groupby((groupablePropertyn),

aggregate(tmpaliasn−1 with aggregationMethodn−1 as tmpaliasn))

/aggregate( tmpaliasn with aggregationMethodn as alias))The order of from clauses has to be compatible with hierarchies referenced from a leveled hierarchy annotation or specified as an unnamed hierarchy in groupby with rollup: lower nodes in a hierarchy need to be mentioned before higher nodes in the same hierarchy. Properties not belonging to any hierarchy can appear at any point in the from clause.

If an aggregate transformation has more than one aggregate expression as parameter that uses the from keyword, then all properties mentioned in from clauses must appear in the same order in all aggregate expressions.

Example 14:

GET ~/Sales?$apply=aggregate(Amount with sum as DailyAverage from Time with average as DailyAverage)

is equivalent to

GET ~/Sales?$apply=groupby((Time),aggregate(Amount with sum as Total)) /aggregate(Total with average as DailyAverage)

and results in the average sales volume per day

{

odata-data-aggregation-ext-v4.0-wd05 Working Draft 05 21 June 2016Standards Track Draft Copyright © OASIS Open 2016. All Rights Reserved. Page 19 of 61

Krause, Gerald, 05/20/20,
ODATA-1201
Krause, Gerald, 05/15/20,
ODATA-968
Page 20:  · Web viewOpen Data (OData) services expose a data model that describes the schema of the service in terms of the Entity Data Model (EDM, see ) and then allows for querying data

"@odata.context": "$metadata#Sales(DailyAverage)", "value": [ { "@odata.id": null, "DailyAverage": 3.428571428571429 } ]}

[3.1.5] Virtual Property $countAggregate Expression $countThe value of the aggregate expression virtual property $count is the number of instances in the input set. It MUST always specify an alias and MUST NOT specify an aggregation method.The result property will have type Edm.Decimal Int64with Scale="0" and sufficient Precision.

Example 15:

GET ~/Sales?$apply=aggregate($count as SalesCount)

results in

{ "@odata.context": "$metadata#Sales(SalesCount)", "value": [ { "@odata.id": null, "SalesCount": 8, ... } ]}

3.2 Transformation topcount The topcount transformation takes two parameters.

The first parameter specifies the number of instances to return in the transformed set. It MUST be an expression expression that can be evaluated on the set level and MUST result in a positive integer.The second parameter specifies the value by which the instances are compared for determining the result set. It MUST be an expression expression that can be evaluated on instances of the input set and MUST result in a primitive numeric valuein a value of a primitive type whose values are totally ordered.The transformation retains the number of instances specified by the first parameter that have the highest values specified by the second expressionparameter. It does not change the order of the instances in the input set.In case the value of the second expression is ambiguous, the service MUST impose a stable ordering before determining the returned instances.

Example 16:

GET ~/Sales?$apply=topcount(2,Amount)

results in

{ "@odata.context": "$metadata#Sales", "value": [ { "ID": 3, "Amount": 4, ... }, { "ID": 4, "Amount": 8, ... } ]}

The result set of topcount has the same structure as the input set.

3.3 Transformation topsum The topsum transformation takes two parameters.

odata-data-aggregation-ext-v4.0-wd05 Working Draft 05 21 June 2016Standards Track Draft Copyright © OASIS Open 2016. All Rights Reserved. Page 20 of 61

Krause, Gerald, 05/18/20,
ODATA-1041
Krause, Gerald, 09/17/18,
ODATA-1182
Krause, Gerald, 18.05.20,
ODATA-1041
Krause, Gerald, 09/17/18,
ODATA-1040
Krause, Gerald, 09/17/18,
ODATA-1040
Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 09/18/18,
ODATA-1215
Krause, Gerald, 06/02/20,
ODATA-1255
Krause, Gerald, 06/02/20,
ODATA-1255
Page 21:  · Web viewOpen Data (OData) services expose a data model that describes the schema of the service in terms of the Entity Data Model (EDM, see ) and then allows for querying data

The first parameter indirectly specifies the number of instances to return in the transformed set. It MUST be an expression expression that can be evaluated on the set level and MUST result in a number.The second parameter specifies the value by which the instances are compared for determining the result set. It MUST be an expression aggregatable expression that can be evaluated on instances of the input set and MUST result in a primitive numeric value.The transformation returns the minimum set of instances that have the highest values specified by the second parameter and whose sum of these values is equal to or greater than the value specified by the first parameter. It does not change the order of the instances in the input set.In case the value of the second expression is ambiguous, the service MUST impose a stable ordering before determining the returned instances.

Example 17:

GET ~/Sales?$apply=topsum(15,Amount)

results in

{ "@odata.context": "$metadata#Sales", "value": [ { "ID": 3, "Amount": 4, ... }, { "ID": 4, "Amount": 8, ... }, { "ID": 5, "Amount": 4, ... } ]}

The result set of topsum has the same structure as the input set.

3.4 Transformation toppercent The toppercent transformation takes two parameters.

The first parameter specifies the number of instances to return in the transformed set. It MUST be an expression expression that can be evaluated on the set level and MUST result in a positive number less than or equal to 100.The second parameter specifies the value by which the instances are compared for determining the result set. It MUST be an expression aggregatable expression that can be evaluated on instances of the input set and MUST result in a primitive numeric value.The transformation returns the minimum set of instances that have the highest values specified by the second parameter and whose cumulative total is equal to or greater than the percentage of the cumulative total of all instances in the input set specified by the first parameter. It does not change the order of the instances in the input set.In case the value of the second expression is ambiguous, the service MUST impose a stable ordering before determining the returned instances.

Example 18:

GET ~/Sales?$apply=toppercent(50,Amount)

results in

{ "@odata.context": "$metadata#Sales", "value": [ { "ID": 3, "Amount": 4, ... }, { "ID": 4, "Amount": 8, ... } ]}

The result set of toppercent has the same structure as the input set.

odata-data-aggregation-ext-v4.0-wd05 Working Draft 05 21 June 2016Standards Track Draft Copyright © OASIS Open 2016. All Rights Reserved. Page 21 of 61

Krause, Gerald, 09/17/18,
ODATA-1040
Krause, Gerald, 09/17/18,
ODATA-1040
Krause, Gerald, 09/17/18,
ODATA-1040
Page 22:  · Web viewOpen Data (OData) services expose a data model that describes the schema of the service in terms of the Entity Data Model (EDM, see ) and then allows for querying data

3.5 Transformation bottomcount The bottomcount transformation takes two parameters.

The first parameter specifies the number of instances to return in the transformed set. It MUST be an expression expression that can be evaluated on the set level and MUST result in a positive integer.The second parameter specifies the value by which the instances are compared for determining the result set. It MUST be an expression expression that can be evaluated on instances of the input set and MUST result in a value of a primitive type whose values are totally orderedin a primitive numeric value.The transformation retains the number of instances specified by the first parameter that have the lowest values specified by the second parameter. It does not change the order of the instances in the input set.In case the value of the second expression is ambiguous, the service MUST impose a stable ordering before determining the returned instances.

Example 19:

GET ~/Sales?$apply=bottomcount(2,Amount)

results in

{ "@odata.context": "$metadata#Sales" "value": [ { "ID": 1, "Amount": 1, ... }, { "ID": 7, "Amount": 1, ... } ]}

The result set of bottomcount has the same structure as the input set.

3.6 Transformation bottomsum The bottomsum transformation takes two parameters.

The first parameter indirectly specifies the number of instances to return in the transformed set. It MUST be an expression expression that can be evaluated on the set level and MUST result in a number.The second parameter specifies the value by which the instances are compared for determining the result set. It MUST be an expression aggregatable expression that can be evaluated on instances of the input set and MUST result in a primitive numeric value.The transformation returns the minimum set of instances that have the lowest values specified by the second parameter and whose sum of these values is equal to or greater than the value specified by the first parameter. It does not change the order of the instances in the input set.In case the value of the second expression is ambiguous, the service MUST impose a stable ordering before determining the returned instances.

Example 20:

GET ~/Sales?$apply=bottomsum(7,Amount)

results in

{ "@odata.context": "$metadata#Sales", "value": [ { "ID": 2, "Amount": 2, ... }, { "ID": 6, "Amount": 2, ... }, { "ID": 7, "Amount": 1, ... }, { "ID": 8, "Amount": 2, ... } ]}

odata-data-aggregation-ext-v4.0-wd05 Working Draft 05 21 June 2016Standards Track Draft Copyright © OASIS Open 2016. All Rights Reserved. Page 22 of 61

Krause, Gerald, 09/17/18,
ODATA-1040
Krause, Gerald, 09/17/18,
ODATA-1040
Krause, Gerald, 09/17/18,
ODATA-1040
Krause, Gerald, 09/17/18,
ODATA-1040
Page 23:  · Web viewOpen Data (OData) services expose a data model that describes the schema of the service in terms of the Entity Data Model (EDM, see ) and then allows for querying data

The result set of bottomsum has the same structure as the input set.

3.7 Transformation bottompercent The bottompercent transformation takes two parameters.

The first parameter indirectly specifies the number of instances to return in the transformed set. It MUST be an expression expression that can be evaluated on the set level and MUST result in a positive number less than or equal to 100.The second parameter specifies the value by which the instances are compared for determining the result set. It MUST be an expression aggregatable expression that can be evaluated on instances of the input set and MUST result in a primitive numeric value.The transformation returns the minimum set of instances that have the lowest values specified by the second parameter and whose cumulative total is equal to or greater than the percentage of the cumulative total of all instances in the input set specified by the first parameter. It does not change the order of the instances in the input set.In case the value of the second expression is ambiguous, the service MUST impose a stable ordering before determining the returned instances.

Example 21:

GET ~/Sales?$apply=bottompercent(50,Amount)

results in

{ "@odata.context": "$metadata#Sales", "value": [ { "ID": 1, "Amount": 1, ... }, { "ID": 2, "Amount": 2, ... }, { "ID": 5, "Amount": 4, ... }, { "ID": 6, "Amount": 2, ... }, { "ID": 7, "Amount": 1, ... }, { "ID": 8, "Amount": 2, ... } ]}

The result set of bottompercent has the same structure as the input set.

3.8 Transformation identity The identity transformation returns its input set in unchanged order.

Example 22:

GET ~/Sales?$apply=identity

3.9 Transformation concat The concat transformation takes two or more parameters, each of which is a sequence of set transformations.It applies each transformation sequence to the input set and concatenates the intermediate result sets in the order of the parameters into the result set, preserving the ordering of the individual result sets as well as the structure of each result instance, potentially leading to an inhomogeneously structured result set.

Example 23:

GET ~/Sales?$apply=concat(topcount(2,Amount), aggregate(Amount))

odata-data-aggregation-ext-v4.0-wd05 Working Draft 05 21 June 2016Standards Track Draft Copyright © OASIS Open 2016. All Rights Reserved. Page 23 of 61

Krause, Gerald, 05/20/20,
ODATA-1158
Krause, Gerald, 09/17/18,
ODATA-1040
Krause, Gerald, 09/17/18,
ODATA-1040
Page 24:  · Web viewOpen Data (OData) services expose a data model that describes the schema of the service in terms of the Entity Data Model (EDM, see ) and then allows for querying data

results in

{ "@odata.context": "$metadata#Sales", "value": [ { "ID": 4, "Amount": 8, ... }, { "ID": 3, "Amount": 4, ... }, { "@odata.context": "$metadata#Sales(Amount)/$entity", "Amount": 24, ... } ]}

Note that two Sales entities with the second highest amount 4 exist in the input set; the entity with ID 3 is included in the result, because the service chose to use the ID property for imposing a stable ordering.

The result set of concat has a mixed form consisting of the structures imposed by the two transformation sequences.

3.10 Transformation groupby The groupby transformation takes one or two parameters and

1. Splits the initial set into subsets where all instances in a subset have the same values for the grouping properties specified in the first parameter,

2. Applies set transformations to each subset according to the second parameter, resulting in a new set of potentially different structure and cardinality,

3. Ensures that the instances in the result set contain all grouping properties with the correct values for the group,

4. Concatenates the intermediate result sets into one result set.

3.10.1 Simple GroupingIn its simplest form the first parameter of groupby specifies the grouping properties, a comma-separated list of one or more single-valued property paths (paths ending in a single-valued primitive, complex, or navigation property) that is enclosed in parentheses. A path may consist of a sequence of single-valued navigation properties,and complex properties and complex collection properties. The same property path SHOULD NOT appear more than once; redundant property paths MAY be considered valid, but MUST NOT alter the meaning of the request. If the property path leads to a single-valued navigation property, this means grouping by the entity-id of the related entities. The optional second parameter is a list of set transformations, separated by forward slashes to express that they are consecutively applied. Transformations may take into account the grouping properties for producing their result, e.g. aggregate removes properties that are used neither for grouping nor for aggregation. If the service is unable to group by same values for any of the specified properties, it MUST reject the request with an error response. It MUST NOT apply any implicit rules to group instances indirectly by another property related to it in some way.

Example 24:

GET ~/Sales?$apply=groupby((Customer/Country,Product/Name), aggregate(Amount with sum as Total))

results in

{ "@odata.context": "$metadata#Sales(Customer(Country),Product(Name),Total)",

odata-data-aggregation-ext-v4.0-wd05 Working Draft 05 21 June 2016Standards Track Draft Copyright © OASIS Open 2016. All Rights Reserved. Page 24 of 61

Krause, Gerald, 09/18/18,
ODATA-1206, ODATA-1404
Krause, Gerald, 05/19/20,
ODATA-1137
Page 25:  · Web viewOpen Data (OData) services expose a data model that describes the schema of the service in terms of the Entity Data Model (EDM, see ) and then allows for querying data

"value": [ { "@odata.id": null, "Customer": { "Country" : "Netherlands" }, "Product": { "Name": "Paper" }, "Total": 3, ... }, { "@odata.id": null, "Customer": { "Country": "Netherlands" }, "Product": { "Name": "Sugar" }, "Total": 2, ... }, { "@odata.id": null, "Customer": { "Country: "USA" }, "Product": { "Name": "Coffee" }, "Total": 12, ... }, { "@odata.id": null, "Customer": { "Country: "USA" }, "Product": { "Name": "Paper" },"Total": 5, ... }, { "@odata.id": null, "Customer": { "Country: "USA" }, "Product": { "Name": "Sugar" }, "Total": 2, ... } ]}

The second parameter can be omitted to request distinct value combinations of the grouping properties.

Example 25:

GET ~/Sales?$apply=groupby((Product/Name,Amount))

and result in

{ "@odata.context": "$metadata#Sales(Product(Name),Amount)", "value": [ { "@odata.id": null, "Product": { "Name": "Coffee" }, "Amount": 4 }, { "@odata.id": null, "Product": { "Name": "Coffee" }, "Amount": 8 }, { "@odata.id": null, "Product": { "Name": "Paper" }, "Amount": 1 }, { "@odata.id": null, "Product": { "Name": "Paper" }, "Amount": 2 }, { "@odata.id": null, "Product": { "Name": "Paper" }, "Amount": 4 }, { "@odata.id": null, "Product": { "Name": "Sugar" }, "Amount": 2 } ]}

Note that the result has the same structure, but not the same content as

GET ~/Sales?$expand=Product($select=Name)&$select=Amount

A groupby transformation affects the structure of the result set similar to $select where each grouping property corresponds to an item in a $select clause.

Grouping by a navigation property is treated as grouping by the references of the navigation targets and adds the canonical URL of the target entities as deferred representation of the navigation property to the result structure, which then can be expanded and projected partially away using the standard query options $expand and $select.

Grouping properties that specify navigation properties are automatically expanded, and the specified properties of that navigation property correspond to properties specified in a $select expand option on the expanded navigation property. The set transformations specified in the second parameter of groupby further affect the structure as described for each transformation; for example, the aggregate transformation adds properties for each aggregate expression.

3.10.2 Grouping with rollup and $allThe rollup grouping operator allows requesting additional levels of aggregation in addition to the most granular level defined by the grouping properties. It can be used instead of a property path in the first parameter of groupby.

The rollup grouping operator has two overloads, depending on the number of parameters.

If used with one parameter, the parameter MUST be the value of the Qualifier attribute of an annotation with term LeveledHierarchy prefixed with the navigation path leading to the annotated entity type. This named hierarchy is used for grouping instances.

odata-data-aggregation-ext-v4.0-wd05 Working Draft 05 21 June 2016Standards Track Draft Copyright © OASIS Open 2016. All Rights Reserved. Page 25 of 61

Krause, Gerald, 05/18/20,
ODATA-971
Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 05/19/20,
ODATA-1137
Page 26:  · Web viewOpen Data (OData) services expose a data model that describes the schema of the service in terms of the Entity Data Model (EDM, see ) and then allows for querying data

If used with two or more parameters, it defines an unnamed leveled hierarchy. The first parameter is the root of the hierarchy defining the coarsest granularity and MUST either be a single-valued property path or the virtual property $all. The other parameters MUST be singe-valued property paths and define consecutively finer-grained levels of the hierarchy. This unnamed hierarchy is used for grouping instances.After resolving named hierarchies, the same property path MUST NOT appear more than once.Grouping with rollup is processed for leveled hierarchies using the following equivalence relationships, in which pn is a property path, T is a transformation, the ellipsis stands in for zero or more property paths, and R stands in for zero or more rollup operators or property paths:

groupby((rollup(p1,…,pn-1,pn),R),T) is equivalent to concat(groupby((p1,…,pn-1,pn,R¿,T),groupby((rollup(p1,…,pn-1),R),T))

groupby((rollup(p1,p2),R),T) is equivalent to concat(groupby((p1,p2,R),T),groupby((p1,R),T))

groupby((rollup($all, p1),R),T) is equivalent to concat(groupby((p1,R),T),groupby((R),T))

groupby((rollup($all,p1)),T) is equivalent to concat(groupby((p1),T),T)

Loosely speaking groupby with rollup splits the input set into groups using all grouping properties, then removes the last property from one of the hierarchies and splits it again using the remaining grouping properties. This is repeated until all of the hierarchies have been used up.

Example 26: rolling up two hierarchies, the first with two levels, the second with three levels:

(rollup(p1,1,p1,2),rollup(p2,1,p2,2,p2,3))

will result in the six groupings

(p1,1,p1,2, p2,1,p2,2,p2,3) (p1,1,p1,2, p2,1,p2,2) (p1,1,p1,2, p2,1) (p1,1, p2,1,p2,2,p2,3) (p1,1, p2,1,p2,2) (p1,1, p2,1)

Note that rollup stops one level earlier than GROUP BY ROLLUP in TSQL, see [TSQL ROLLUP], unless the virtual property $all is used as the hierarchy root level. Loosely speaking the root level is never rolled up.Ordering of rollup instances within detail instances is up to the service if no $orderby is given, otherwise at the position determined by $orderby.

Example 27: answering the second question in section 2.4

GET ~/Sales?$apply=groupby((rollup(Customer/Country,Customer/Name), rollup(Product/Category/Name,Product/Name), Currency/Code), aggregate(Amount with sum as Total))

results in seven entities for the finest grouping level

{"@odata.context":"$metadata#Sales(Customer(Country,Name),Product(Category(Na

me),Name),Total,Currency(Code))",

odata-data-aggregation-ext-v4.0-wd05 Working Draft 05 21 June 2016Standards Track Draft Copyright © OASIS Open 2016. All Rights Reserved. Page 26 of 61

Page 27:  · Web viewOpen Data (OData) services expose a data model that describes the schema of the service in terms of the Entity Data Model (EDM, see ) and then allows for querying data

"value": [ { "@odata.id": null, "Customer": { "Country": "USA", "Name": "Joe" }, "Product": { "Category": { "Name": "Non-Food" }, "Name": "Paper" }, "Total": 1, "Currency": { "Code": "USD" }, ... }, ...

plus additional fifteen rollup entities for subtotals: five without customer name

{ "@odata.id": null, "Customer": { "Country": "USA" }, "Product": { "Category": { "Name": "Food" }, "Name": "Sugar" }, "Total": 2, "Currency": { "Code": "USD" }, ... }, ...

six without product name

{ "@odata.id": null, "Customer": { "Country": "USA", "Name": "Joe" }, "Product": { "Category": { "Name": "Food" } }, "Total": 6, "Currency": { "Code": "USD" }, ... }, ...

and four with neither customer nor product name

{ "@odata.id": null, "Customer": { "Country": "USA" }, "Product": { "Category": { "Name": "Food" } }, "Total": 14, "Currency": { "Code": "USD" }, ... }, ... ]}

Note that the absence of one or more properties of the result structure imposed by the surrounding OData context allows distinguishing rollup entities from other entities.

3.11 Transformation filter The filter transformation takes a Boolean expression expression that could also be passed as a $filter system query option to its input set and returns all instances for which this expression evaluates to true.

Example 28:

GET ~/Sales?$apply=filter(Amount gt 3)

results in

{ "@odata.context": "$metadata#Sales", "value": [ { "ID": 3, "Amount": 4, ... }, { "ID": 4, "Amount": 8, ... }, { "ID": 5, "Amount": 4, ... } ]}

The result set of filter has the same structure as the input set.

3.12 Transformation nestThe nest transformation takes one of two forms that are distinguished by the type of the first parameter.

odata-data-aggregation-ext-v4.0-wd05 Working Draft 05 21 June 2016Standards Track Draft Copyright © OASIS Open 2016. All Rights Reserved. Page 27 of 61

Heiko Theißen, 01/19/21,
ODATA-1404
Krause, Gerald, 09/17/18,
ODATA-1040
Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 05/19/20,
ODATA-1137
Page 28:  · Web viewOpen Data (OData) services expose a data model that describes the schema of the service in terms of the Entity Data Model (EDM, see ) and then allows for querying data

In the first form, the first parameter is a non-empty path. The path consists of segments separated by a forward slash (/). Segments are either names of single- or collection-valued complex properties, single- or collection-valued complex instance annotations, or type-cast segments consisting of the qualified name of a structured type that is derived from the type identified by the preceding path segment to reach properties defined on the derived type. The path can have at its end an additional segment or consist entirely of a segment that is

either a navigation property to expand the related entity or entities or an entity-valued instance annotation to expand the related entity or entities

optionally followed by a type-cast segment to expand only related entities of that derived type or one of its sub-types. (Compare this to the definition in [OData-URL, section 5.1.3].) The sub-path starting with the last segment that is not a type-cast is called the final sub-path in this definition.The mandatory second and further optional parameters are transformation sequences. Each transformation sequence with the possible exception of the last MUST be followed by the as keyword followed by a SimpleIdentifier ([OData-CSDL, section 17.2]), called an alias. These aliases MUST NOT collide with names of properties in the instances containing the final sub-path nor with each other. If the final sub-path addresses single instances (rather than collections), each transformation sequence MUST consist of either one identity transformation or one nest transformation. In this way, the transformation sequence can be applied to the single instance by applying it to a collection containing the single instance and taking as result the single instance from the result set.The path MUST be well-defined for all instances in the input set, otherwise the request is rejected. The result set is constructed by expanding the input set with the navigation property or entity-valued instance annotation that appear in the path (cf. [OData-URL, section 5.1.3]) and then, for each occurrence of the final sub-path in the expanded input set, processing the resource that is addressed by that occurrence as follows:

1. For each transformation sequence with alias, a dynamic property is added to the instance containing the resource. The name of the dynamic property is the alias. The value of the dynamic property is the result of the transformation sequence applied to the resource. The dynamic property carries as control information the context URL of the result of the transformation sequence, as if the dynamic property were a navigation property.

2. If there is no transformation sequence without alias and the final sub-path starts with a navigation property or entity-valued instance annotation, the resource is removed from its containing instance.

Example 29:

GET ~/Customers?$apply=nest(Sales, filter(Amount gt 3) as FilteredSales)

results in

{ "@odata.context": "$metadata#Customers", "value": [ { "ID": "C1", "Name": "Joe", "Country": "USA", "[email protected]": "#Sales", "FilteredSales": [{ "ID": "3", "Amount": 4, ... }]}, { "ID": "C2", "Name": "Sue", "Country": "USA", "[email protected]": "#Sales", "FilteredSales": [{ "ID": "4", "Amount": 8, ... }, { "ID": "5", "Amount": 4, ... }]}, { "ID": "C3", "Name": "Sue", "Country": "Netherlands", "[email protected]": "#Sales", "FilteredSales": []}, { "ID": "C4", "Name": "Luc", "Country": "France", "[email protected]": "#Sales", "FilteredSales": []} ]}

odata-data-aggregation-ext-v4.0-wd05 Working Draft 05 21 June 2016Standards Track Draft Copyright © OASIS Open 2016. All Rights Reserved. Page 28 of 61

Page 29:  · Web viewOpen Data (OData) services expose a data model that describes the schema of the service in terms of the Entity Data Model (EDM, see ) and then allows for querying data

(If Sales was a collection-valued complex property of type SalesModel.SalesComplexType, the context would be "[email protected]": "#Collection(SalesModel.SalesComplexType)".)

3. If the last transformation sequence is without alias, it is applied to the resource.

Example 30:

GET ~/Customers?$apply=nest(Sales,filter(Amount gt 3))

results in

{ "@odata.context": "$metadata#Customers", "value": [ { "ID": "C1", "Name": "Joe", "Country": "USA", "Sales": [{ "ID": "3", "Amount": 4, ... }]}, { "ID": "C2", "Name": "Sue", "Country": "USA", "Sales": [{ "ID": "4", "Amount": 8, ... }, { "ID": "5", "Amount": 4, ... }]}, { "ID": "C3", "Name": "Sue", "Country": "Netherlands", "Sales": []}, { "ID": "C4", "Name": "Luc", "Country": "France", "Sales": []} ]}

If, during the execution of a nest transformation, step 3 is executed, its effects on the result set are visible to the next transformation that uses this as input set, if it expands the same path again. These effects are represented in the context URL of the response payload. (See Example 63.)The second form tries to extend the first to the case of an empty path. It processes the input set itself because an empty path has no final sub-path.In the second form, no path is provided. The mandatory first and further optional parameters are all transformation sequences followed by the as keyword followed by an alias. These aliases MUST NOT collide with each other.The result set consists of a single entity having one dynamic property per transformation sequence. The name of the dynamic property is the alias. The value of the dynamic property is the collection resulting from the transformation sequence applied to the input set.

Example 31:

GET Sales?$apply = groupby((Product/Category/ID), nest(groupby((Customer/ID)) as Customers))

results in

{"@odata.context":"$metadata#Sales(Product(Category(ID)),Customers(Customer(I

D)))", "value": [ { "Product": { "Category": { "ID": "PG1"} }, "Customers": [ { "Customer": { "ID": "C1"} }, { "Customer": { "ID": "C2" } }, { "Customer": { "ID": "C3" } } ] }, { "Product": { "Category": { "ID": "PG2"} }, "Customers": [ { "Customer": { "ID": "C1"} }, { "Customer": { "ID": "C2" } }, { "Customer": { "ID": "C3" } } ] } ]}

odata-data-aggregation-ext-v4.0-wd05 Working Draft 05 21 June 2016Standards Track Draft Copyright © OASIS Open 2016. All Rights Reserved. Page 29 of 61

Page 30:  · Web viewOpen Data (OData) services expose a data model that describes the schema of the service in terms of the Entity Data Model (EDM, see ) and then allows for querying data

3.13 Transformation expand The expand transformation takes a navigation property path that could also be passed as a $expand system query option as its first parameter. The second parameter specifies a transformation that will be applied to the related entities. It can either be a nested expand transformation or a filter transformation; the latter MUST be provided for the innermost expand transformation. The optional second parameter can be either a filter transformation that will be applied to the related entities or an expand transformation. An arbitrary number of expand transformations can be passed as additional parameters to achieve multi-level expansion.The result set is the input set with the specified navigation property expanded according to the specified expand options.

Example 32:

GET ~/Customers?$apply=expand(Sales,filter(Amount gt 3))

results in

{ "@odata.context": "$metadata#Customers", "value": [ { "ID": "C1", "Name": "Joe", "Country": "USA", "Sales": [{ "ID": 3, "Amount": 4, ... }]}, { "ID": "C2", "Name": "Sue", "Country": "USA", "Sales": [{ "ID": 4, "Amount": 8, ... }, { "ID": 5, "Amount": 4, ... }]}, { "ID": "C3", "Name": "Sue", "Country": "Netherlands", "Sales": []}, { "ID": "C4", "Name": "Luc", "Country": "France", "Sales": []} ]}

The result has the same structure and content as

GET ~/Customers?$expand=Sales($filter=Amount gt 3)

An expand transformation affects the structure of the result set in the same way as an $expand option for the first parameter, with nested $expand options for the optional nested expand transformations.

Example 33: nested expand transformations

GET ~/Categories?$apply=expand(Products,expand(Sales,filter(Amount gt 3)))

results in

{ "@odata.context": "$metadata#CustomersCategories", "value": [ { "ID": "PG1", "Name": "Food", "Products": [ { "ID": "P1", "Name": "Sugar", "Color": "White", "Sales": [] }, { "ID": "P2", "Name": "Coffee", "Color": "Brown", "Sales": [ { "ID": 3, "Amount": 4, ... }, { "ID": 4, "Amount": 8, ... } ] } ] }, { "ID": "PG2", "Name": "Non-Food", "Products": [ { "ID": "P3", "Name": "Paper", "Color": "White", "Sales": [ { "ID": 5, "Amount": 4, ... }, { "ID": 8, "Amount": 2, ... } ] }, { "ID": "P4", "Name": "Pencil", "Color": "Black", "Sales": [] } ]

odata-data-aggregation-ext-v4.0-wd05 Working Draft 05 21 June 2016Standards Track Draft Copyright © OASIS Open 2016. All Rights Reserved. Page 30 of 61

Krause, Gerald, 09/17/18,
ODATA-972
Krause, Gerald, 06/02/20,
ODATA-1369
Krause, Gerald, 05/25/20,
ODATA-1068Change already reflected in ABNF
Page 31:  · Web viewOpen Data (OData) services expose a data model that describes the schema of the service in terms of the Entity Data Model (EDM, see ) and then allows for querying data

} ]}

[3.13] Transformation search The search transformation takes a search expression that could also be passed as a $search system query option to its input set and returns all entities that match this search expression.

Example 34: assuming that free-text search on Sales takes the related product name into account,

GET ~/Sales?$apply=search(coffee)

results in

{ "@odata.context": "$metadata#Sales", "value": [ { "ID": 3, "Amount": 4, ... }, { "ID": 4, "Amount": 8, ... } ]}

The result set of search has the same structure as the input set.

3.14 Transformation compute The compute transformation takes a comma-separated list of one or more compute expressions as parameters. A compute expression is an aggregatable expression expression valid in a $filter system query option on the input set that results in a simple value, followed by the as keyword, followed by a SimpleIdentifier (see [OData-CSDL, section 17.2]), called an alias. This alias MUST NOT collide with names of properties in the input set or with other aliases introduced in the same compute transformation.

The result set is constructed by copying the instances of the input set and adding one dynamic property per compute expression to each instance of the input set. The name of the added property is the alias following the as keyword. The value of the property is the value of the compute expression evaluated on that instance. The type of the property is determined by the rules for evaluating $filter expressions and numeric promotion defined in [OData-URL]. The JSON representation of these dynamic properties will include odata.type control information where required by [OData-JSON]. The values of properties copied from the input set are not changed, nor is the order of instances changed.

Example 35:

GET ~/Sales?$apply=compute(Amount mul Product/TaxRate as Tax)

results in

{ "@odata.context": "$metadata#Sales", "value": [ { "ID": 1, ..., "Amount": 1, "Tax": 0.14 }, { "ID": 2, ..., "Amount": 2, "Tax": 0.12 }, { "ID": 3, ..., "Amount": 4, "Tax": 0.24 }, { "ID": 4, ..., "Amount": 8, "Tax": 0.48 }, { "ID": 5, ..., "Amount": 4, "Tax": 0.56 }, { "ID": 6, ..., "Amount": 2, "Tax": 0.12 }, { "ID": 7, ..., "Amount": 1, "Tax": 0.14 }, { "ID": 8, ..., "Amount": 2, "Tax": 0.28 } ]}

odata-data-aggregation-ext-v4.0-wd05 Working Draft 05 21 June 2016Standards Track Draft Copyright © OASIS Open 2016. All Rights Reserved. Page 31 of 61

Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 05/18/20,
ODATA-1038
Page 32:  · Web viewOpen Data (OData) services expose a data model that describes the schema of the service in terms of the Entity Data Model (EDM, see ) and then allows for querying data

3.15 Transformation orderby The orderby transformation takes an expression that could also be passed as a $orderby system query option and returns the instances of the input set in the same order $orderby would produce for the given expression. A service supporting this transformation MUST at least offer sorting by property paths, including dynamic properties, with both suffixes asc and desc.

Example 36:

GET ~/Sales?$apply=groupby((Product/Name), aggregate(Amount with sum as Total)) /orderby(Total desc)

results in

{ "@odata.context": "$metadata#Sales(Product(Name),Total)", "value": [ { "Product": { "Name": "Coffee" }, "Total": 12, ... }, { "Product": { "Name": "Paper" }, "Total": 8, ... }, { "Product": { "Name": "Sugar" }, "Total": 4, ... } ]}

The result set of orderby has the same structure as the input set.

3.16 Transformation top The top transformation takes a non-negative integer n as argument and returns the first n entities of the input set, if the input set contains more than n entities, otherwise the entire input set. The entities in the result are in the same order as they occur in the input set.The transformations preceding top in the sequence must first produce a certain order among the entities, and then keep this order stable for the input set of top.

Example 37:

GET ~/Sales?$apply=orderby(Customer/Name desc)/top(2)

results in

{ "@odata.context": "$metadata#Sales" "value": [ { "ID": 4, "Amount": 8, ... }, { "ID": 5, "Amount": 4, ... } ]}

3.17 Transformation skip The skip transformation takes a non-negative integer n as argument that excludes the first n entities of the input set from the result. It returns all remaining entities in the same order as they occur in the input set.The transformations preceding skip in the sequence must first produce a certain order among the entities, and then keep this order stable for the input set of skip.

Example 38:

GET ~/Sales?$apply=orderby(Customer/Name desc)/skip(2)/top(2)

odata-data-aggregation-ext-v4.0-wd05 Working Draft 05 21 June 2016Standards Track Draft Copyright © OASIS Open 2016. All Rights Reserved. Page 32 of 61

Krause, Gerald, 05/26/20,
ODATA-1158
Krause, Gerald, 05/26/20,
ODATA-1158
Krause, Gerald, 05/26/20,
ODATA-1157
Page 33:  · Web viewOpen Data (OData) services expose a data model that describes the schema of the service in terms of the Entity Data Model (EDM, see ) and then allows for querying data

results in

{ "@odata.context": "$metadata#Sales" "value": [ { "ID": 6, "Amount": 2, ... }, { "ID": 7, "Amount": 1, ... } ]}

3.18 Transformation nest The nest transformation takes a comma-separated list of one or more nest expressions as parameters. A nest expression is a sequence of set transformations, followed by the as keyword, followed by a SimpleIdentifier (see [OData-CSDL, section 17.2]) , called an alias. This alias MUST NOT collide with names of properties in the input set or with other aliases introduced in the same nest transformation.

The result set consists of a single entity having one dynamic property per nest expression. The name of the added property is the alias following the as keyword. The value of the property is the result collection of the transformation sequence in the nest expression applied to the input set.

Example 39:

GET Sales?$apply = groupby((Product/Category/ID), nest(groupby((Customer/ID)) as Customers))

results in

{"@odata.context":"$metadata#Sale

s(Product(Category(ID)),Customers(Customer(ID)))", "value": [ { "Product": { "Category": { "ID": "PG1"} }, "Customers": [ { "Customer": { "ID": "C1"} }, { "Customer": { "ID": "C2" } }, { "Customer": { "ID": "C3" } } ] }, { "Product": { "Category": { "ID": "PG2"} }, "Customers": [ { "Customer": { "ID": "C1"} }, { "Customer": { "ID": "C2" } }, { "Customer": { "ID": "C3" } } ] } ]}

[3.16] Filter Function isdefinedProperties that are not explicitly mentioned in aggregate or groupby are considered to have been aggregated away and are treated as having the null value in $filter expressions.

The filter function isdefined can be used to determine whether a property has been aggregated away. It takes a single-valued property path as its only parameter and returns true if the property has a defined value for the aggregated entity. A property with a defined value can still have the null value; it can represent a grouping of null values, or an aggregation that results in a null value.

Example 40:Product has been aggregated away, causing an empty result

GET ~/Sales?$apply=aggregate(Amount with sum as Total) &$filter=isdefined(Product)

results in

{ "@odata.context": "$metadata#Sales(Total)",

odata-data-aggregation-ext-v4.0-wd05 Working Draft 05 21 June 2016Standards Track Draft Copyright © OASIS Open 2016. All Rights Reserved. Page 33 of 61

Krause, Gerald, 06/02/20,
ODATA-1372
Krause, Gerald, 06/02/20,
ODATA-1372
Krause, Gerald, 05/26/20,
ODATA-1169, ODATA-1185
Page 34:  · Web viewOpen Data (OData) services expose a data model that describes the schema of the service in terms of the Entity Data Model (EDM, see ) and then allows for querying data

"value": []}

3.19[3.17] Function aggregateThe aggregate function allows to use aggregated values in expressions. It takes a single parameter accepting an aggregate-function expression and returns the aggregated value of type Edm.PrimitiveType as the result from applying the aggregate-function expression on an input collection provided by the context.An aggregate-function expression offers the same capabilities as an aggregate expression defined for the aggregate transformation except that it does not support aggregation on related entities, and it never includes an alias for introducing a dynamic property containing the aggregated value.The function can be applied in any context accepting an expression. Within $apply, the function is applied as part of an argument expression to a transformation, and the input set of that transformation is the input collection for the function. Outside of $apply, the function may be applied in requests querying collections in an argument expression for a system query option and a lambda operator. Inside a system query option, the collection on which it operates is used as input collection for the function. This is also valid for nested query options used as arguments, like $filter in $expand. Inside a lambda predicate expression the input collection for the aggregate function depends on the path prefix used in the aggregateFunctionExpr (see [OData-Agg-ABNF]): For a path expression starting with the lambda variable the input collection is given by the target of the navigation path prepended to the lambda operator; for a path expression starting with $it the collection addressed by the resource path is the input collection; for other path expressions the input collection is given by the collection of instances at the origin of the navigation path prepended to the lambda operator.

Example 41:

GET ~/Products?$filter=Sales/any(p:aggregate(p/Amount with sum) gt 5)

results in

{ "@odata.context": "$metadata#Product", "value": [ { "ID": "P2", ... }, { "ID": "P3", ... } ]}

3.20 Evaluating $applyThe new system query option $apply is evaluated first, then the other system query options are evaluated, if applicable, on the result of $apply in their normal order (see [OData-Protocol, section 11.2.1]). If the result is a collection, $filter, $orderby, $expand and $select work as usual on properties that are defined on the output set after evaluating $apply.

Properties that have been aggregated away in a result entity are not represented, even if the properties are listed in $select or $expand. In $filter they are treated as having the null value, and in $orderby as having a value that is even lower than null, i.e. instances for which a property has been rolled up appear before instances that have a null value for that property when ordering ascending. In subsequent transformations, they are treated like in $filter as having the null value.

On resource paths ending in /$count the system query option $apply is evaluated on the set identified by the resource path without the /$count segment, the result is the plain-text number of items in the result of $apply. This is similar to the combination of /$count and $filter.

odata-data-aggregation-ext-v4.0-wd05 Working Draft 05 21 June 2016Standards Track Draft Copyright © OASIS Open 2016. All Rights Reserved. Page 34 of 61

Krause, Gerald, 09/18/18,
ODATA-1161
Krause, Gerald, 26.05.20,
ODATA-1244Change already reflected in ABNF
Page 35:  · Web viewOpen Data (OData) services expose a data model that describes the schema of the service in terms of the Entity Data Model (EDM, see ) and then allows for querying data

The $count system query option is evaluated after $apply, the annotation @odata.count contains the number of items in the result of $apply.

Providers MAY support $count, $top and $skip together with rollup, in which case rollup instances are counted identically to detail instances, i.e. $skip=5 skips the first five instances, independently of whether some of them are rollup entities.If a provider cannot satisfy a request using $apply, it MUST respond with 501 Not Implemented and a human-readable error message.

3.21[3.18] Evaluating $apply as an Expand OptionThe new system query option $apply can be used as an expand option to inline the result of aggregating related entities. The rules for evaluating $apply are applied in the context of the expanded navigation, i.e. $apply is evaluated first, and other expand options on the same navigation property are evaluated on the result of $apply.

Example 42: products with aggregated sales:

GET Products?$expand=Sales($apply=aggregate(Amount with sum as Total))

results in

{ "@odata.context":"$metadata#Products(Salees(AmountTotal))", "value": [ { "Name": "Coffee", "Color": "Brown", "TaxRate": 0.06, "Sales": [ { "@odata.id": null, "Total": 12, ... } ] }, { "Name": "Paper", "Color": "White", "TaxRate": 0.14, "Sales": [ { "@odata.id": null, "Total": 8, ... } ] }, { "Name": "Pencil", "Color": "Black", "TaxRate": 0.14, "Sales": [ { "@odata.id": null, "Total": null } ] }, { "Name": "Sugar", "Color": "White", "TaxRate": 0.06, "Sales": [ { "@odata.id": null, "Total": 4, ... } ] } ] }

3.22[3.19] ABNF for Extended URL ConventionsThe normative ABNF construction rules for this specification are defined in [OData-Agg-ABNF]. They incrementally extend the rules defined in [OData-ABNF].

odata-data-aggregation-ext-v4.0-wd05 Working Draft 05 21 June 2016Standards Track Draft Copyright © OASIS Open 2016. All Rights Reserved. Page 35 of 61

Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 05/19/20,
ODATA-1137
Michael Pizzo, 06/05/19,
OData-1307
Page 36:  · Web viewOpen Data (OData) services expose a data model that describes the schema of the service in terms of the Entity Data Model (EDM, see ) and then allows for querying data

4 Representation of Aggregated InstancesAggregated instances are based on the structure of the individual instances from which they have been calculated, so the structure of the results fits into the data model of the service. Properties that have been aggregated away are not represented at all in the aggregated instances.Dynamic properties introduced through an alias or with custom aggregates are represented as defined by the response format. In particular, this includes a type annotation as additional control information to disambiguate the type of the represented value where required by the response format.Aggregated instances are logically instances of the declared type of the collection identified by the resource path of the request. If the resource path identifies a collection of entities, the aggregated instances are also entities. These aggregated entities can be transient or persistent. Transient entities don’t possess an edit link or read link, and in the JSON representation of OData 4.0 payloads are marked with "@odata.id": null, which can be omitted in 4.01 payloads, see [OData-JSON]. Edit links or read links of persistent entities MUST encode the necessary information to re-retrieve that particular aggregate value. How the necessary information is exactly encoded is not part of this specification. Only the boundary conditions defined in [OData-Protocol], sections 4.1 and 4.2 MUST be met.The context URL describes the superset of properties occurring in entities of the aggregated result set.

Example 43: looking again to the sample request for getting sales amounts per product and country presented in section 3.10.1 (Example 24):

GET ~/Sales?$apply=groupby((Customer/Country,Product/Name), aggregate(Amount with sum as Total))

will return corresponding metadata as shown here for a single transient aggregated entity:

{ "@odata.context":"$metadata#Sales(Customer(Country),Product(Name),Total)", "value": [ { "@odata.id": null, "Customer": { "Country": "Netherlands" }, "Product": { "Name": "Paper" }, "Total": 3, ... }, ... ]}

odata-data-aggregation-ext-v4.0-wd05 Working Draft 05 21 June 2016Standards Track Draft Copyright © OASIS Open 2016. All Rights Reserved. Page 36 of 61

Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 09/17/18,
ODATA-1159
Krause, Gerald, 05/27/20,
ODATA-1279
Krause, Gerald, 05/27/20,
ODATA-1137
Page 37:  · Web viewOpen Data (OData) services expose a data model that describes the schema of the service in terms of the Entity Data Model (EDM, see ) and then allows for querying data

5 Cross-Joins and AggregationOData supports querying related entities through defining navigation properties in the data model. These navigation paths help guide simple consumers in understanding and navigating relationships.In some cases, however, requests need to span entity sets with no predefined associations. Such requests can be sent to the special resource $crossjoin instead of an individual entity set. The cross join of a list of entity sets is the Cartesian product of the listed entity sets, represented as a collection of complex type instances that have a navigation property with cardinality to-one for each participating entity set, and queries across entity sets can be formulated using these navigation properties. See [OData-URL] for details.Where useful navigations exist it is beneficial to expose those as explicit navigation properties in the model, but the ability to pose queries that span entity sets not related by an association provides a mechanism for advanced consumers to use more flexible join conditions.

Example 44: if Sales had a string property ProductID instead of the navigation property Product, a “join” between Sales and Products could be accessed via the $crossjoin resource

GET ~/$crossjoin(Products,Sales) ?$expand=Products($select=Name),Sales($select=Amount) &$filter=Products/ID eq Sales/ProductID

results in

{ "@odata.context": "$metadata#Collection(Edm.ComplexType)", "value": [ { "Products": { "Name": "Paper" }, "Sales": { "Amount": 1 } }, { "Products": { "Name": "Sugar" }, "Sales": { "Amount": 2 } }, ... ]}

Example 45: using the $crossjoin resource for aggregate queries

GET ~/$crossjoin(Products,Sales) ?$apply=filter(Products/ID eq Sales/ProductID) /groupby((Products/Name), aggregate(Sales(Amount with sum as Total)))

results in

{ "@odata.context": "$metadata#Collection(Edm.ComplexType)", "value": [ { "Products": { "Name": "Coffee" }, "Sales": { "Total": 12, ... } }, { "Products": { "Name": "Paper" }, "Sales": { "Total": 8, ... } }, { "Products": { "Name": "Sugar" }, "Sales": { "Total": 4, ... } } ]}

The entity container may be annotated in the same way as entity sets to express which aggregate queries are supported, see section 6.

odata-data-aggregation-ext-v4.0-wd05 Working Draft 05 21 June 2016Standards Track Draft Copyright © OASIS Open 2016. All Rights Reserved. Page 37 of 61

Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 05/19/20,
ODATA-1137
Page 38:  · Web viewOpen Data (OData) services expose a data model that describes the schema of the service in terms of the Entity Data Model (EDM, see ) and then allows for querying data

6 Vocabulary for Data AggregationThe following terms are defined in the vocabulary for data aggregation [OData-VocAggr].

6.1 Aggregation Capabilities The term ApplySupported can be applied to an entity container or to structured types and describes the aggregation capabilities of the entity container or of collections of instances of the annotated structured types. If present, it implies that instances of the annotated structured type, or of structured types used in the annotated entity container, can contain dynamic properties as an effect of $apply even if they do not specify the OpenType attribute, see [OData-CSDL]. The term has a complex type with the following properties:

The Transformations collection lists all supported set transformations. Allowed values are the names of the standard transformations introduced in section 3Allowed values are the names of the standard transformations aggregate, topcount, topsum, toppercent, bottomcount, bottomsum, bottompercent, identity, concat, groupby, filter, and expand, search, and compute, or a and namespace-qualified names identifying a service-defined bindable function. If Transformations is omitted the server supports all transformations defined by this specification.

The CustomAggregationMethods collection lists supported custom aggregation methods. Allowed values are namespace-qualified names identifying service-specific aggregation methods. If omitted, no custom aggregation methods are supported.

Rollup specifies whether the service supports no rollup, only a single rollup hierarchy, or multiple rollup hierarchies in a groupby transformation. If omitted, multiple rollup hierarchies are supported.

PropertyRestrictions specifies whether all properties can be used in groupby and aggregate. If not specified, or specified with a value of false, all properties can be grouped and aggregated. If specified with a value of true clients have to check which properties are tagged as Groupable or Aggregatable.

All properties of ApplySupported are optional, so it can be used as a tagging annotation to signal unlimited support of aggregation.

Example 46: an entity container supporting everything defined in this specification.

<EntityContainer Name="SalesData"> <Annotation Term="Aggregation.ApplySupported" /> ...</EntityContainer>

6.2 Property Annotations

6.2.1 Groupable PropertiesIf a structured type is annotated with ApplySupported or used within an entity container that is annotated with ApplySupported, and the ApplySupported annotation has a value of true for PropertyRestrictions, only those properties that are annotated with the tagging term Groupable can be used in groupby.

6.2.2 Aggregatable PropertiesIf a structured type is annotated with ApplySupported or used within an entity container that is annotated with ApplySupported, and the ApplySupported annotation has a value of true for

odata-data-aggregation-ext-v4.0-wd05 Working Draft 05 21 June 2016Standards Track Draft Copyright © OASIS Open 2016. All Rights Reserved. Page 38 of 61

Krause, Gerald, 09/17/18,
ODATA-1186
Page 39:  · Web viewOpen Data (OData) services expose a data model that describes the schema of the service in terms of the Entity Data Model (EDM, see ) and then allows for querying data

PropertyRestrictions, only those properties that are annotated with the tagging term Aggregatable can be used in aggregate.

6.2.3 Supported Aggregation MethodsIf a structured type is annotated with ApplySupported or used within an entity container that is annotated with ApplySupported, all aggregation methods can be applied to an aggregatable property by default unless it is annotated with SupportedAggregationMethods that lists the subset of those aggregation methods that the service supports for this property.

6.2.4 Recommended Aggregation MethodIf a structured type is annotated with ApplySupported or used within an entity container that is annotated with ApplySupported, the service may advertise a recommended aggregation method for an aggregatable property to the consumer by annotating it with RecommendedAggregationMethod.

6.2.5 Custom AggregatesThe term CustomAggregate allows defining dynamic properties that can be used in aggregate. No assumptions can be made on how the values of these custom aggregates are calculated, and which input values are used.When applied to a structured type, the annotation specifies custom aggregates that are available for collections of instances of that structured type. When applied to an entity container, the annotation specifies custom aggregates whose input set may span multiple entity sets within the container.A custom aggregate is identified by the value of the Qualifier attribute when applying the term. The value of the Qualifier attribute is the name of the dynamic property. The name MUST NOT collide with the names of other custom aggregates of the same model element. The value of the annotation is a string with the qualified name of a primitive type or type definition in scope that specifies the type returned by the custom aggregate. If the custom aggregate is associated with a structured type, the value of the Qualifier attribute MAY be identical to the name of a declared property of the structured type. In this case, the value of the annotation MUST have the same value as the Type attribute of the declared property. This is typically done when the custom aggregate is used as a default aggregate for that property. In this case, the name refers to the custom aggregate within an aggregate expression without a with clause, and to the property in all other cases.If the custom aggregate is associated with an entity container, the value of the Qualifier attribute MUST NOT collide with the names of any entity sets defined in the entity container.

Example 47: Sales forecasts are modeled as a custom aggregate of the Sales entity type because it belongs there. For the budget, there is no appropriate structured type, so it is modeled as a custom aggregate of the SalesData entity container.

<Annotations Target="SalesModel.Sales"> <Annotation Term="Aggregation.CustomAggregate" Qualifier="Forecast" String="Edm.Decimal" /></Annotations>

<Annotations Target="SalesModel.SalesData"> <Annotation Term="Aggregation.CustomAggregate" Qualifier="Budget" String="Edm.Decimal" /></Annotations>

These custom aggregates can be used in the aggregate transformation:

GET ~/Sales?$apply=groupby((Time/Month),aggregate(Forecast))

odata-data-aggregation-ext-v4.0-wd05 Working Draft 05 21 June 2016Standards Track Draft Copyright © OASIS Open 2016. All Rights Reserved. Page 39 of 61

Krause, Gerald, 05/18/20,
ODATA-1072
Krause, Gerald, 05/18/20,
ODATA-1072PR
Page 40:  · Web viewOpen Data (OData) services expose a data model that describes the schema of the service in terms of the Entity Data Model (EDM, see ) and then allows for querying data

and:

GET ~/$crossjoin(Time)?$apply=groupby((Time/Year),aggregate(Budget))

6.2.6[6.2.4] Context-Defining PropertiesSometimes the value of a property or custom aggregate is only well-defined within the context given by values of other properties, e.g. a postal code together with its country, or a monetary amount together with its currency unit. These context-defining properties can be listed with the term ContextDefiningProperties whose type is a collection of property paths.

If present, the context-defining properties SHOULD be used as grouping properties when aggregating the annotated property or custom aggregate, or alternatively be restricted to a single value by a pre-filter operation. Services MAY respond with 400 Bad Request if the context-defining properties are not sufficiently specified for calculating a meaningful aggregate value.

6.2.7[6.2.5] Annotation ExampleExample 48: This simplified Sales entity type has a single aggregatable property Amount whose context is defined by the Code property of the related Currency, and a custom aggregate Forecast with the same context. The Code property of Currency is groupable. All other properties are neither groupable nor aggregatable.

<EntityType Name="Currency"> <Key> <PropertyRef Name="Code" /> </Key> <Property Name="Code" Type="Edm.String"> <Annotation Term="Aggregation.Groupable" /> </Property> <Property Name="Name" Type="Edm.String"> <Annotation Term="Core.IsLanguageDependent" /> </Property></EntityType>

<EntityType Name="Sales"> <Key> <PropertyRef Name="ID" /> </Key> <Property Name="ID" Type="Edm.String" Nullable="false" />

<Property Name="Amount" Type="Edm.Decimal" Scale="variable"> <Annotation Term="Aggregation.Aggregateable" /> <Annotation Term="Aggregation.ContextDefiningProperties"> <Collection> <PropertyPath>Currency/Code</PropertyPath> </Collection> </Annotation> </Property>

<NavigationProperty Name="Currency" Type="SalesModel.Currency" Nullable="false"> <Annotation Term="Aggregation.Groupable" /> </NavigationProperty>

<Annotation Term="Aggregation.CustomAggregate" Qualifier="Forecast" String="Edm.Decimal"> <Annotation Term="Aggregation.ContextDefiningProperties"> <Collection> <PropertyPath>Currency/Code</PropertyPath> </Collection> </Annotation>

odata-data-aggregation-ext-v4.0-wd05 Working Draft 05 21 June 2016Standards Track Draft Copyright © OASIS Open 2016. All Rights Reserved. Page 40 of 61

Page 41:  · Web viewOpen Data (OData) services expose a data model that describes the schema of the service in terms of the Entity Data Model (EDM, see ) and then allows for querying data

</Annotation></EntityType>

<EntityContainer Name="SalesData"> <Annotation Term="Aggregation.ApplySupported"> <Record> <PropertyValue Property="PropertyRestrictions" Bool="true" /> </Record> </Annotation>

<EntitySet Name="Sales" EntityType="SalesModel.Sales" /> <EntitySet Name="Currencies" EntityType="SalesModel.Currency" /> </EntityContainer>

6.3 HierarchiesA hierarchy is an arrangement of groupable properties whose values are represented as being “above”, “below”, or “at the same level as” one another. A hierarchy can be leveled or recursive.

6.3.1 Leveled HierarchyA leveled hierarchy has a fixed number of levels each of which is represented by a groupable property. The values of a lower-level property depend on the property value of the level above. A leveled hierarchy of an entity type is described with the term LeveledHierarchy that lists the properties used to form the hierarchy.The order of the collection is significant: it lists the properties representing the levels, starting with the root level (coarsest granularity) down to the lowest level of the hierarchy.The term LeveledHierarchy can only be applied to entity types, and the applying Annotation element MUST specify the Qualifier attribute. The value of the Qualifier attribute can be used to reference the hierarchy in grouping with rollup.

6.3.2 Recursive HierarchyA recursive hierarchy organizes the values of a single groupable property as nodes of a tree structure. This structure does not need to be as uniform as a leveled hierarchy. It is described by a complex term RecursiveHierarchy with the properties:

The NodeProperty contains the path to the identifier of the node. The ParentNavigationProperty allows navigation to the entity representing the parent node. The optional ChildrenNavigationProperty allows navigation to the entities representing the

children nodes. The optional DistanceFromRootProperty contains the path to a property that contains the

number of edges between the node and the root node. The optional IsLeafProperty contains the path to a Boolean property that indicates whether

the node is a leaf of the hierarchy.The term RecursiveHierarchy can only be applied to entity types, and the applying Annotation element MUST specify the Qualifier attribute. The value of the Qualifier attribute can be used to reference the hierarchy in Hierarchy Filter FunctionsHierarchy Functions.

[6.3.2.1] Hierarchy Filter FunctionsFor testing the position of a given entity instance in a recursive hierarchy annotated to the entity’s type, the Aggregation vocabulary defines functions that can be applied to any entity in $filter expressions:

isroot returns true if and only if the value of the node property of the specified hierarchy is the root of the hierarchy,

odata-data-aggregation-ext-v4.0-wd05 Working Draft 05 21 June 2016Standards Track Draft Copyright © OASIS Open 2016. All Rights Reserved. Page 41 of 61

Krause, Gerald, 06/02/20,
ODATA-1372
Krause, Gerald, 06/02/20,
ODATA-1372
Krause, Gerald, 06/02/20,
ODATA-1372
Krause, Gerald, 06/02/20,
ODATA-1372
Krause, Gerald, 05/20/20,
ODATA-839PR
Page 42:  · Web viewOpen Data (OData) services expose a data model that describes the schema of the service in terms of the Entity Data Model (EDM, see ) and then allows for querying data

isdescendant returns true if and only if the value of the node property of the specified hierarchy is a descendant of the given parent node with a distance of less than or equal to the optionally specified maximum distance or, if selected, optionally the parent node itself,

isancestor returns true if and only if the value of the node property of the specified hierarchy is an ancestor of the given child node with a distance of less than or equal to the optionally specified maximum distance or, if selected, optionally the child node itself,

issibling returns true if and only if the value of the node property of the specified hierarchy has the same parent node as the specified node,

isleaf returns true if and only if the value of the node property of the specified hierarchy has no descendants.

6.3.3 Hierarchy ExamplesExample 49: leveled hierarchies for products and time, and a recursive hierarchy for the sales organizations

<edmx:Edmx xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx" Version="4.0"> <edmx:Reference Uri="http://docs.oasis-open.org/odata/odata-data-aggregation-ext/v4.0/cs01/vocabularies/Org.OData.Aggregation.V1.xml"> <edmx:Include Alias="Aggregation" Namespace="Org.OData.Aggregation.V1" /> </edmx:Reference> <edmx:DataServices> <Schema xmlns="http://docs.oasis-open.org/odata/ns/edm" Alias="SalesModel" Namespace="org.example.odata.salesservice"> <Annotations Target="SalesModel.Product"> <Annotation Term="Aggregation.LeveledHierarchy" Qualifier="ProductHierarchy"> <Collection> <PropertyPath>Category/Name</PropertyPath> <PropertyPath>Name</PropertyPath> </Collection> </Annotation> </Annotations>

<Annotations Target="SalesModel.Time"> <Annotation Term="Aggregation.LeveledHierarchy" Qualifier="TimeHierarchy"> <Collection> <PropertyPath>Year</PropertyPath> <PropertyPath>Quarter</PropertyPath> <PropertyPath>Month</PropertyPath> </Collection> </Annotation> </Annotations>

<Annotations Target="SalesModel.SalesOrganization"> <Annotation Term="Aggregation.RecursiveHierarchy" Qualifier="SalesOrgHierarchy"> <Record> <PropertyValue Property="NodeProperty" PropertyPath="ID" /> <PropertyValue Property="ParentNavigationProperty" PropertyPath="Superordinate" /> </Record> </Annotation> </Annotations> </Schema> </edmx:DataServices></edmx:Edmx>

odata-data-aggregation-ext-v4.0-wd05 Working Draft 05 21 June 2016Standards Track Draft Copyright © OASIS Open 2016. All Rights Reserved. Page 42 of 61

Krause, Gerald, 05/19/20,
ODATA-1136
Krause, Gerald, 05/25/20,
ODATA-1142PR
Krause, Gerald, 05/19/20,
ODATA-1136PR
Krause, Gerald, 05/25/20,
ODATA-1142
Page 43:  · Web viewOpen Data (OData) services expose a data model that describes the schema of the service in terms of the Entity Data Model (EDM, see ) and then allows for querying data

The recursive hierarchy SalesOrgHierarchy can be used in functions with the $filter system query option.

Example 50: requesting all organizations below EMEA

GET ~/SalesOrganizations? $filter= $it/Aggregation.isdescendant(Hierarchy='SalesOrgHierarchy',Node='EMEA')

results in

{ "@odata.context": "$metadata#SalesOrganizations", "value": [ { "ID": "EMEA Central", "Name": "EMEA Central" }, { "ID": "Sales Netherland", "Name": "Sales Netherland" }, { "ID": "Sales Germany", "Name": "Sales Germany" }, { "ID": "EMEA South", "Name": "EMEA South" }, ... { "ID": "EMEA North", "Name": "EMEA North" }, ... ]}

Example 51: requesting just those organizations directly below EMEA

GET SalesOrganizations?$filter= $it/Aggregation.isdescendant(Hierarchy='SalesOrgHierarchy', Node='EMEA',MaxDistance=1)

results in

{ "@odata.context": "$metadata#SalesOrganizations", "value": [ { "ID": "EMEA Central", "Name": "EMEA Central" }, { "ID": "EMEA South", "Name": "EMEA South" }, { "ID": "EMEA North", "Name": "EMEA North" }, ... ]}

Example 52: just the lowest-level organizations

GET SalesOrganizations?$filter= $it/Aggregation.isleaf(Hierarchy='SalesOrgHierarchy')

results in

{ "@odata.context": "$metadata#SalesOrganizations","value": [

{ "ID": "Sales Office London", "Name": "Sales Office London" }, { "ID": "Sales Office New York", "Name": "Sales Office New York" }, ... ]}

Example 53: the lowest-level organizations including their superordinate's ID

GET SalesOrganizations?$filter= $it/Aggregation.isleaf(Hierarchy='SalesOrgHierarchy') &$expand=Superordinate($select=ID)

odata-data-aggregation-ext-v4.0-wd05 Working Draft 05 21 June 2016Standards Track Draft Copyright © OASIS Open 2016. All Rights Reserved. Page 43 of 61

Page 44:  · Web viewOpen Data (OData) services expose a data model that describes the schema of the service in terms of the Entity Data Model (EDM, see ) and then allows for querying data

results in

{ "@odata.context": "$metadata#SalesOrganizations(*,Superordinate(ID))", "value": [ { "ID": "Sales Office London", "Name": "Sales Office London", "Superordinate: { "ID": "EMEA United Kingdom" } }, { "ID": "Sales Office New York", "Name": "Sales Office New York", "Superordinate: { "ID": "US East" } }, ... ]}

Example 54: retrieving the sales IDs involving sales organizations from EMEA can be requested by

GET Sales?$select=ID&$filter= SalesOrganization/Aggregation.isdescendant(Hierarchy='SalesOrgHierarchy', Node='EMEA')

results in

{ "@odata.context": "$metadata#Sales(ID)", "value": [ { "ID": 6 }, { "ID": 7 }, { "ID": 8 } ]}

6.4 Actions and Functions on Aggregated Entities Bound actions and functions may or may not be applicable to aggregated entities. By default such bindings are not applicable to aggregated entities. Actions or functions annotated with the term AvailableOnAggregates are applicable to (a subset of the) aggregated entities under specific conditions:

The RequiredProperties collection lists all properties that must be available in the aggregated entities; otherwise, the annotated function or action will be inapplicable.

Example 55: assume the product is an implicit input for a function bindable to Sales, then aggregating away the product makes this function inapplicable.

Calculating a set of aggregated entities and invoking an action on them cannot be accomplished with a single request, because the action URL cannot be constructed by the client. It is also impossible to construct a URL that calculates a single aggregated entity and applies a function or action on it. Consequently, applicable bound actions or functions on a single aggregated entity, or bound actions on a collection of aggregated entities MUST be advertised in the response to make them available to clients. A client is then able to request the aggregated entities in a first request and invoke the action or function in a follow-up request using the advertised target URL.

Example 56: full representation of an action applicable to a collection of aggregated entities, and an action that is applicable to one of the entities in the collection. The string <properties in $apply> is a stand-in for the list of properties describing the shape of the result set

{"@odata.context": "$metadata#Sales(<properties in $apply>)","@odata.readLink": "http://.../aggregated-stuff2143248437259843","#Model.ColAction": {

"title": "Do something on this collection", "target": "http://.../aggregated-stuff2143248437259843/Model.ColAction" },"value": [ {

odata-data-aggregation-ext-v4.0-wd05 Working Draft 05 21 June 2016Standards Track Draft Copyright © OASIS Open 2016. All Rights Reserved. Page 44 of 61

Page 45:  · Web viewOpen Data (OData) services expose a data model that describes the schema of the service in terms of the Entity Data Model (EDM, see ) and then allows for querying data

"@odata.id": "aggregated-stuff2143248437259843-1", "#Model.SingleAction": {

"title": "Do something on this entity", "target": "http://.../aggregated-stuff2143248437259843-1/Model.SingleAction" }, ... }, ...]

}

Services advertising the availability of functions or actions via the term AvailableOnAggregates MUST provide read links or edit links for aggregated entities, see section 4.

odata-data-aggregation-ext-v4.0-wd05 Working Draft 05 21 June 2016Standards Track Draft Copyright © OASIS Open 2016. All Rights Reserved. Page 45 of 61

Page 46:  · Web viewOpen Data (OData) services expose a data model that describes the schema of the service in terms of the Entity Data Model (EDM, see ) and then allows for querying data

7 ExamplesThe following examples show some common aggregation-related questions that can be answered by combining the transformations defined in chapter 3.

7.1 Requesting Distinct ValuesGrouping without specifying a set transformation returns the distinct combination of the grouping properties.

Example 57:

GET ~/Customers?$apply=groupby((Name))

results in

{ "@odata.context": "$metadata#Customers(Name)", "value": [ { "@odata.id": null, "Name": "Luc" }, { "@odata.id": null, "Name": "Joe" }, { "@odata.id": null, "Name": "Sue" } ]}

Note that "Sue" appears only once although the customer base contains two different Sues.

Aggregation is also possible across related entities.

Example 58: customers that bought something

GET ~/Sales?$apply=groupby((Customer/Name))

results in

{ "@odata.context": "$metadata#Sales(Customer(Name))", "value": [ { "@odata.id": null, "Customer": { "Name": "Joe" } }, { "@odata.id": null, "Customer": { "Name": "Sue" } } ]}

The result has the same structure as a standard OData request that expands the navigation properties and selects the data properties specified in groupby and aggregate.

GET ~/Sales?$expand=Customer($select=Name)

Note that "Luc" does not appear in the aggregated result as he hasn’t bought anything and therefore there are no sales entities that refer/navigate to Luc.

However, even though both Sues bought products, only one "Sue" appears in the aggregate result. Including properties that guarantee the right level of uniqueness in the grouping can repair that.

Example 59:

GET ~/Sales?$apply=groupby((Customer/Name,Customer/ID))

results in

{ "@odata.context": "$metadata#Sales(Customer(Name,ID))",

odata-data-aggregation-ext-v4.0-wd05 Working Draft 05 21 June 2016Standards Track Draft Copyright © OASIS Open 2016. All Rights Reserved. Page 46 of 61

Page 47:  · Web viewOpen Data (OData) services expose a data model that describes the schema of the service in terms of the Entity Data Model (EDM, see ) and then allows for querying data

"value": [ { "@odata.id": null, "Customer": { "Name": "Joe", "ID": "C1" } }, { "@odata.id": null, "Customer": { "Name": "Sue", "ID": "C2" } }, { "@odata.id": null, "Customer": { "Name": "Sue", "ID": "C3" } } ]}

This could also have been formulated as

GET ~/Sales?$apply=groupby((Customer)) &$expand=Customer($select=Name,ID)

Example 60: Grouping by navigation property Customer is treated as groupby((Customer/$ref))

GET ~/Sales?$apply=groupby((Customer))

results in

{ "@odata.context": "$metadata#Sales(Customer)", "value": [ { "Customer": { "@odata.id": "Customers('C1')" } }, { "Customer": { "@odata.id": "Customers('C2')" } }, { "Customer": { "@odata.id": "Customers('C3')" } } ]}

Grouping by a navigation property adds the deferred representation of the navigation property to the result structure, which then can be expanded and projected partially away using the standard query options $expand and $select.

Note: the typical representation of a deferred navigation property is a URL “relative” to the source entity, e.g. ~/Sales(1)/Customer. This has the benefit that this URL doesn’t change if the sales entity would be associated to a different customer. For aggregated entities this would actually be a drawback, so the representation MUST be the canonical URL of the target entity, i.e. ~/Customers('C1') for the first entity in the above result.

Example 61: the first question in the motivating example in section 2.4, which customers bought which products, can now be expressed as

GET ~Sales?$apply=groupby((Customer/Name,Customer/ID,Product/Name))

and results in

{ "@odata.context": "$metadata#Sales(Customer(Name,ID),Product(Name))", "value": [ { "@odata.id": null, "Customer": { "Name": "Joe", "ID": "C1" }, "Product": { "Name": "Coffee"} }, { "@odata.id": null, "Customer": { "Name": "Joe", "ID": "C1" }, "Product": { "Name": "Paper" } }, { "@odata.id": null, "Customer": { "Name": "Joe", "ID": "C1" }, "Product: { "Name: "Sugar" } }, { "@odata.id": null, "Customer": { "Name": "Sue", "ID": "C2" }, "Product: { "Name": "Coffee"} }, { "@odata.id": null, "Customer": { "Name": "Sue", "ID": "C2" }, "Product": { "Name": "Paper" } }, { "@odata.id": null, "Customer": { "Name": "Sue", "ID": "C3" }, "Product": { "Name": "Paper" } }, { "@odata.id": null, "Customer": { "Name": "Sue", "ID": "C3" }, "Product": { "Name": "Sugar" } } ]}

odata-data-aggregation-ext-v4.0-wd05 Working Draft 05 21 June 2016Standards Track Draft Copyright © OASIS Open 2016. All Rights Reserved. Page 47 of 61

Krause, Gerald, 05/18/20,
Krause, Gerald, 06/08/20,
ODATA-971
Page 48:  · Web viewOpen Data (OData) services expose a data model that describes the schema of the service in terms of the Entity Data Model (EDM, see ) and then allows for querying data

7.2 Standard Aggregation Methods The client may specify one of the predefined aggregation methods min, max, sum, average, and countdistinct, or a custom aggregation method, to aggregate an aggregatable property. Expressions defining an aggregate method MUST specify an alias. The aggregated values are returned in a dynamic property whose name is determined by the alias.

Example 62:

GET ~/Products?$apply=groupby((Name), aggregate(Sales/Amount with sum as Total))

results in

{ "@odata.context": "$metadata#Products(Name,Sales(Total))", "value": [ { "@odata.id": null, "Name": "Coffee", "Sales": [ { "Total": 12, ... } ] }, { "@odata.id": null, "Name": "Paper", "Sales": [ { "Total": 8, ... } ] }, { "@odata.id": null, "Name": "Pencil", "Sales": [ { "Total": null } ] }, { "@odata.id": null, "Name": "Sugar", "Sales": [ { "Total": 4, ... } ] } ]}

Note that the base set of the request is Products, so there is a result item for product Pencil even though there are no sales item. As aggregate returns exactly one result item even if there are no items to be aggregated, the Sales navigation property’s value is an array with one element representing the sum over no input values, which is null.

Example 63: careful observers will notice that the above amounts have been aggregated across currencies, which is semantically wrong. Yet it is the correct response to the question asked, so be careful what you ask for. The semantically meaningful question

GET ~/Products?$apply=nest(Sales, groupby((Currency/Code), aggregate(Amount with sum as Total)))GET ~/Products?$apply=groupby((Name,Sales/Currency/Code), aggregate(Sales(/Amount with sum as Total)))

results in

{ "@odata.context": "$metadata#Products(Name,Sales(Total,Currency(Code)))", "value": [ { "@odata.id": null, "Name": "Coffee", "Sales": [ { "Total": 12, "Currency": { "Code": "USD" }, ... } ] }, { "@odata.id": null, "Name: "Paper", "Sales": [ { "Total": 3, "Currency": { "Code": "EUR" }, ... }, { "Total": 5, "Currency": { "Code": "USD" }, ... } ] }, { "@odata.id": null, "Name: "Pencil", "Sales": [] }, { "@odata.id": null, "Name: "Sugar", "Sales": [ { "Total": 2, "Currency": { "Code": "EUR" }, ... }, { "Total": 2, "Currency": { "Code": "USD" }, ... } ] } ]}

nest transformations can be nested.

odata-data-aggregation-ext-v4.0-wd05 Working Draft 05 21 June 2016Standards Track Draft Copyright © OASIS Open 2016. All Rights Reserved. Page 48 of 61

Heiko Theißen, 01/18/21,
ODATA-1404
Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 06/02/20,
ODATA-945
Heiko Theißen, 10/30/20,
ODATA-1404
Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 06/02/20,
ODATA-945
Page 49:  · Web viewOpen Data (OData) services expose a data model that describes the schema of the service in terms of the Entity Data Model (EDM, see ) and then allows for querying data

Example 64: nested nest transformations

GET ~/Categories?$apply=nest(Products, nest(Sales,filter(Amount gt 3)))

results in

{ "@odata.context": "$metadata#Categories", "value": [ { "ID": "PG1", "Name": "Food", "Products": [ { "ID": "P1", "Name": "Sugar", "Color": "White", "Sales": [] }, { "ID": "P2", "Name": "Coffee", "Color": "Brown", "Sales": [ { "ID": 3, "Amount": 4, ... }, { "ID": 4, "Amount": 8, ... } ] } ] }, { "ID": "PG2", "Name": "Non-Food", "Products": [ { "ID": "P3", "Name": "Paper", "Color": "White", "Sales": [ { "ID": 5, "Amount": 4, ... } ] }, { "ID": "P4", "Name": "Pencil", "Color": "Black", "Sales": [] } ] } ]}

Note that navigation properties are "expanded" in a left-outer-join fashion, starting from the target of the aggregation request, before grouping the entities for aggregation. To match the $expand structure, non-existing targets of a collection-valued navigation property are represented as empty collection and a non-existing single-valued navigation target as null. Afterwards the results are “folded back” to match the cardinality of the navigation properties.

Example 65:

GET ~/Customers?$apply=groupby((Country,Sales/Product/Name))

returns the different products sold per country:

{ "@odata.context": "$metadata#Customers(Country,Sales(Product(Name)))", "value": [ { "@odata.id": null, "Country": "Netherlands", "Sales": [ { "Product": { "Name": "Paper" } }, { "Product": { "Name": "Sugar" } } ] }, { "@odata.id": null, "Country": "USA", "Sales": [ { "Product": { "Name": "Coffee" } }, { "Product": { "Name": "Paper" } }, { "Product": { "Name": "Sugar" } } ] }, { "Country": "France", "Sales": [ ] } ]}

Example 66:

GET ~/Sales?$apply=groupby((Customer/Country), aggregate(Amount with average as AverageAmount))

results in

{ "@odata.context": "$metadata#Sales(Customer(Country),AverageAmount)",

odata-data-aggregation-ext-v4.0-wd05 Working Draft 05 21 June 2016Standards Track Draft Copyright © OASIS Open 2016. All Rights Reserved. Page 49 of 61

Krause, Gerald, 05/15/20,
ODATA-904
Krause, Gerald, 05/18/20,
ODATA-904
Gerald Krause, 09/17/18,
ODATA-972
Gerald Krause, 06/02/20,
ODATA-1369
Page 50:  · Web viewOpen Data (OData) services expose a data model that describes the schema of the service in terms of the Entity Data Model (EDM, see ) and then allows for querying data

"value": [ { "@odata.id": null, "Customer": { "Country": "Netherlands" }, "AverageAmount": 1.6666667 }, { "@odata.id": null, "Customer": { "Country": "USA" }, "AverageAmount": 3.8 } ]}

If the example model would contain a list of hobbies per customer, with Hobbies a collection of strings, the number of different hobbies across the customer base could be requested. A navigation property followed by a /$count segment is a valid expression in the context that declares the navigation property, so the result property is placed in the same context as the navigation property.

Example 67:

GET ~/Products?$apply=groupby((Name), aggregate(Sales/$count with sum as SalesCount))

results in

{ "@odata.context": "$metadata#Products(Name,SalesCount)", "value": [ { "@odata.id": null, "Name": "Coffee", "SalesCount": 2, ... }, { "@odata.id": null, "Name": "Paper", "SalesCount": 4, ... }, { "@odata.id": null, "Name": "Pencil", "SalesCount": 0, ... }, { "@odata.id": null, "Name": "Sugar", "SalesCount": 2, ... } ]}

Note that this differs from the placement of an aggregated property in a related entity: the aggregated property has the same navigation path as the original value.

Example 68: the result properties for Sales/$count and Sales(Amount…) are placed differently

GET ~/Products?$apply=groupby((Name), aggregate(Sales/$count with sum as SalesCount, Sales(Amount with sum as TotalAmount)))

results in

{ "@odata.context": "$metadata#Products(Name,SalesCount,Sales(TotalAmount))", "value": [ { "@odata.id": null, "Name": "Coffee", "SalesCount": 2, ..., "Sales": [ { "TotalAmount": 12, ... } ] }, { "@odata.id": null, "Name": "Paper", "SalesCount": 4, ..., "Sales": [ { "TotalAmount": 8, ... } ] }, { "@odata.id": null, "Name": "Pencil", "SalesCount": 0, ..., "Sales": [ { "TotalAmount": null } ] }, { "@odata.id": null, "Name": "Sugar", "SalesCount": 2, ..., "Sales": [ { "TotalAmount": 4, ... } ] } ]}

To place the number of instances in a group next to other aggregated values, the virtual propertyaggregate expression $count can be used:

Example 69: the result properties for Sales/$count and Sales/Amount are placed differently

GET ~/Products?$apply=groupby((Name),aggregate(Sales($count as SalesCount), Sales(Amount with sum as TotalAmount)))

odata-data-aggregation-ext-v4.0-wd05 Working Draft 05 21 June 2016Standards Track Draft Copyright © OASIS Open 2016. All Rights Reserved. Page 50 of 61

Krause, Gerald, 06/02/20,
ODATA-1255
Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 05/26/20,
ODATA-1255
Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 05/26/20,
ODATA-1255Change already reflected in ABNF
Page 51:  · Web viewOpen Data (OData) services expose a data model that describes the schema of the service in terms of the Entity Data Model (EDM, see ) and then allows for querying data

results in

{ "@odata.context": "$metadata#Products(Name,Sales(SalesCount,TotalAmount))", "value": [ { "@odata.id": null, "Name": "Coffee", "Sales": [ { "SalesCount": 2, "TotalAmount": 12, ... } ] }, { "@odata.id": null, "Name": "Paper", "Sales": [ { "SalesCount": 4, "TotalAmount": 8, ... } ] }, { "@odata.id": null, "Name": "Pencil", "Sales": [ { "SalesCount": 0, "TotalAmount": null, ... } ] }, { "@odata.id": null, "Name": "Sugar", "Sales": [ { "SalesCount": 2, "TotalAmount": 4, ... } ] } ]}

7.3 Requesting Custom AggregatesCustom aggregates are defined through the CustomAggregate annotation. They can be associated with either an entity type or an entity container. A custom aggregate can be used by specifying the name of the custom aggregate in the aggregate clause.

Example 70:

GET ~/Sales?$apply=groupby((Customer/Country), aggregate(Amount with sum as Actual,Forecast))

results in

{ "@odata.context": "$metadata#Sales(Customer(Country),Actual,Forecast))","value": [

{ "@odata.id": null, "Customer": { "Country": "Netherlands" }, "Actual": 5, "Forecast": 4, ... },

{ "@odata.id": null, "Customer": { "Country": "USA" }, "Actual": 19, "Forecast": 21, ... }

]}

The introduced dynamic properties MUST always be in the same set as the original property.

Example 71:

GET ~/Products?$apply=groupby((Name), aggregate(Sales(Amount with sum as Actual), Sales/Forecast))

results in

{ "@odata.context": "$metadata#Products(Name,Sales(Actual,Forecast))", "value": [ { "@odata.id":null,"Name": "Coffee", "Sales": [ { "Actual": 12, "Forecast": 6, ... } ] }, { "@odata.id":null,"Name": "Paper", "Sales": [ { "Actual": 8, "Forecast": 2, ... } ] }, { "@odata.id":null,"Name": "Pencil", "Sales": [] }, { "@odata.id":null,"Name": "Sugar", "Sales": [ { "Actual": 4, "Forecast": 7, ... } ] } ]}

odata-data-aggregation-ext-v4.0-wd05 Working Draft 05 21 June 2016Standards Track Draft Copyright © OASIS Open 2016. All Rights Reserved. Page 51 of 61

Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 05/19/20,
ODATA-1137
Page 52:  · Web viewOpen Data (OData) services expose a data model that describes the schema of the service in terms of the Entity Data Model (EDM, see ) and then allows for querying data

When associated with an entity type a custom aggregate MAY have the same name as a property of the entity with the same type as the type returned by the custom aggregate. This is typically done when the aggregate is used as a default aggregate for that property.

Example 72 A custom aggregate can be defined with the same name as a property of the same type in order to define a default aggregate for that property.

GET ~/Sales?$apply=groupby((Customer/Country),aggregate(Amount))

results in

{ "@odata.context": "$metadata#Sales(Customer(Country),Amount)", "value": [ { "@odata.id":null, "Customer": { "Country": "Netherlands" }, "Amount": 5, ... }, { "@odata.id":null, "Customer": { "Country": "USA" }, "Amount": 19, ... } ]}

7.4 AliasingA property can be aggregated in multiple ways, each with a different alias.

Example 73:

GET ~/Sales?$apply=groupby((Customer/Country), aggregate(Amount with sum as Total, Amount with average as AvgAmt))

results in

{ "@odata.context": "$metadata#Sales(Customer(Country),Total,AvgAmt))","value": [

{ "@odata.id": null, "Customer": { "Country": "Netherlands" }, "Total": 5, "AvgAmt": 1.6666667, ... },

{ "@odata.id": null, "Customer": { "Country": "USA" }, "Total": 19, "AvgAmt": 3.8, ... }

]}

The introduced dynamic properties MUST always be in the same set as the original property.

Example 74:

GET ~/Products?$apply=groupby((Name), aggregate(Sales(Amount with sum as Total), Sales(Amount with average as AvgAmt)))

results in

{ "@odata.context": "$metadata#Products(Name,Sales(Total,AvgAmt))", "value": [ { "@odata.id":null,"Name": "Coffee", "Sales": [ { "Total": 12, "AvgAmt": 6, ... } ] }, { "@odata.id":null,"Name": "Paper", "Sales": [ { "Total": 8, "AvgAmt": 2, ... } ] }, { "@odata.id":null,"Name": "Pencil", "Sales": [ { "Total": null, "AvgAmt": null } ] }, { "@odata.id":null,"Name": "Sugar", "Sales": [ { "Total": 4, "AvgAmt": 2, ... } ] } ]

odata-data-aggregation-ext-v4.0-wd05 Working Draft 05 21 June 2016Standards Track Draft Copyright © OASIS Open 2016. All Rights Reserved. Page 52 of 61

Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 05/19/20,
ODATA-1137
Page 53:  · Web viewOpen Data (OData) services expose a data model that describes the schema of the service in terms of the Entity Data Model (EDM, see ) and then allows for querying data

}

There is no hard distinction between groupable and aggregatable properties: the same property can be aggregated and used to group the aggregated results.

Example 75:

GET ~/Sales?$apply=groupby((Amount),aggregate(Amount with sum as Total))

will return all distinct amounts appearing in sales orders and how much money was made with deals of this amount

{ "@odata.context": "$metadata#Sales(Amount,Total)", "value": [ { "@odata.id": null, "Amount": 1, "Total": 2, ... }, { "@odata.id": null, "Amount": 2, "Total": 6, ... }, { "@odata.id": null, "Amount": 4, "Total": 8, ... }, { "@odata.id": null, "Amount": 8, "Total": 8, ... } ]}

7.5 Combining Transformations per GroupDynamic property names may be reused in different transformation sequences passed to concat.

Example 76: to get the best-selling product per country with sub-totals for every country, the partial results of a transformation sequence and a groupby transformation are concatenated:

GET ~/Sales?$apply=concat( groupby((Customer/Country,Product/Name,Currency/Code), aggregate(Amount with sum as Total)) /groupby((Customer/Country,Currency/Code), topcount(1,Total)), groupby((Customer/Country,Currency/Code), aggregate(Amount with sum as Total)))

results in

{ "@odata.context": "$metadata#Sales(Customer(Country),Product(Name),Total,Currency(Code))", "value": [ { "@odata.id": null, "Customer": { "Country": "USA" }, "Product": { "Name": "Coffee" }, "Total": 12, "Currency": { "Code": "USD" }, ... }, { "@odata.id": null, "Customer": { "Country": "Netherlands" }, "Product": { "Name": "Paper" }, "Total": 3, "Currency": { "Code": "EUR" }, ... }, { "@odata.id": null, "Customer": { "Country": "USA" }, "Total": 19, "Currency": { "Code": "USD" }, ... }, { "@odata.id": null, "Customer": { "Country": "Netherlands" }, "Total": 5, "Currency": { "Code": "EUR" }, ... } ]}

Example 77: transformation sequences are also useful inside groupby: To get the aggregated amount by only considering the top two sales amounts per product and county:

GET ~/Sales?$apply=groupby((Customer/Country,Product/Name,Currency/Code),

odata-data-aggregation-ext-v4.0-wd05 Working Draft 05 21 June 2016Standards Track Draft Copyright © OASIS Open 2016. All Rights Reserved. Page 53 of 61

Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 09/17/18,
ODATA-1160
Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 05/19/20,
ODATA-1137
Page 54:  · Web viewOpen Data (OData) services expose a data model that describes the schema of the service in terms of the Entity Data Model (EDM, see ) and then allows for querying data

topcount(2,Amount)/aggregate(Amount with sum as Total))

results in

{ "@odata.context": "$metadata#Sales(Customer(Country),Product(Name),Total,Currency(Code))", "value": [ { "@odata.id": null, "Customer": { "Country": "Netherlands" }, "Product": { "Name": "Paper" }, "Total": 3, "Currency": { "Code": "EUR" }, ... }, { "@odata.id": null, "Customer": { "Country": "Netherlands" }, "Product": { "Name": "Sugar" }, "Total": 2, "Currency": { "Code": "EUR" }, ... }, { "Customer": { "Country": "USA" }, "Product": { "Name": "Sugar" }, "Total": 2, "Currency": { "Code": "USD" }, ... }, { "@odata.id": null, "Customer": { "Country": "USA" }, "Product": { "Name": "Coffee" }, "Total": 12, "Currency": { "Code": "USD" }, ... }, { "@odata.id": null, "Customer": { "Country": "USA" }, "Product": { "Name": "Paper" }, "Total": 5, "Currency": { "Code": "USD" }, ... } ]}

7.6 Model Functions as Set TransformationsExample 78: as a variation of the example shown in the previous section, a query for returning the best-selling product per country and the total amount of the remaining products can be formulated with the help of a model function.

For this purpose, the model includes a definition of a TopCountAndBalance function that accepts the count for the top entities in the given input set not to be considered for the balance:

<edm:Function Name="TopCountAndBalance" ReturnType="Collection(Edm.EntityType)" IsBound="true"> <edm:Parameter Name="EntityCollection" Type="Collection(Edm.EntityType)"/> <edm:Parameter Name="Count" Type="Edm.Int16"/> <edm:Parameter Name="Property" Type="Edm.String"/></edm:Function>

The function takes the name of a numeric property as a parameter, retains those entities that topcount also would retain, and replaces the remaining entities by a single aggregated entity, where only the numeric property has a defined value being the aggregated value over those remaining entities:

GET ~/Sales?$apply=groupby((Customer/Country,Product/Name), aggregate(Amount with sum as Total))

/groupby((Customer/Country), Self.TopCountAndBalance(Count=1,Property='Total'))

results in

{ "@odata.context": "$metadata#Sales(Customer(Country),Product(Name),Total)", "value": [ { "@odata.id": null, "Customer": { "Country": "Netherlands" },

odata-data-aggregation-ext-v4.0-wd05 Working Draft 05 21 June 2016Standards Track Draft Copyright © OASIS Open 2016. All Rights Reserved. Page 54 of 61

Krause, Gerald, 05/25/20,
ODATA-1225
Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 09/17/18,
ODATA-905
Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 05/19/20,
ODATA-1137
Page 55:  · Web viewOpen Data (OData) services expose a data model that describes the schema of the service in terms of the Entity Data Model (EDM, see ) and then allows for querying data

"Product": { "Name": "Paper" }, "Total": 3, ... }, { "@odata.id": null, "Customer": { "Country": "Netherlands" }, "Product": { "Name": "**Other**" }, "Total": 2, ... }, { "@odata.id": null, "Customer": { "Country": "USA" }, "Product": { "Name": "Coffee" }, "Total": 12, ... }, { "@odata.id": null, "Customer": { "Country": "USA" }, "Product": { "Name": "**Other**" }, "Total": 7, ...5 } ]}

Note that these two entities get their values for the Country property from the groupby transformation, which ensures that they contain all grouping properties with the correct values.

7.7 Controlling Aggregation per Rollup LevelConsumers may specify a different aggregation method per level for every property passed to rollup as a hierarchy level below the root level.

Example 79: get the average of the overall amount by month per product.

Using a transformation sequence:

GET ~/Sales?$apply=groupby((Product/ID,Product/Name,Time/Month), aggregate(Amount with sum) as Total)) /groupby((Product/ID,Product/Name), aggregate(Total with average as AverageAmount))

Using from:

GET ~/Sales?$apply=groupby((Product/ID,Product/Name), aggregate(Amount with sum as MonthlyAverage from Time/Month with average as MonthlyAverage))

Example 80: for an aggregate entity set listing the total sales amounts per customer and country, the rollup shall produce additional instances for the average total sales amount of customers per country and the average of that average (which is a bit boring because the example data doesn’t have two countries with the same currency)

GET ~/Sales?$apply=groupby((rollup($all,Customer/Country,Customer/ID), Currency/Code), aggregate(Amount with sum as CustomerCountryAverage from Customer/ID with average from Customer/Country with average as CustomerCountryAverage))

results in

{ "@odata.context": "$metadata#Sales(Customer(Country,ID),CustomerCountryAverage,Currency(Code))",

odata-data-aggregation-ext-v4.0-wd05 Working Draft 05 21 June 2016Standards Track Draft Copyright © OASIS Open 2016. All Rights Reserved. Page 55 of 61

Krause, Gerald, 05/20/20,
ODATA-1201
Krause, Gerald, 05/20/20,
ODATA-1201Change already reflected in ABNF
Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 09/17/18,
ODATA-905
Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 05/19/20,
ODATA-1137
Page 56:  · Web viewOpen Data (OData) services expose a data model that describes the schema of the service in terms of the Entity Data Model (EDM, see ) and then allows for querying data

"value": [ { "@odata.id": null, "Customer": { "Country": "USA", "ID": "C1" }, "CustomerCountryAverage": 7, "Currency": { "Code": "USD" } }, { "@odata.id": null, "Customer": { "Country": "USA", "ID": "C2" }, "CustomerCountryAverage": 12, "Currency": { "Code": "USD" } }, { "@odata.id": null, "Customer": { "Country": "USA" }, "CustomerCountryAverage": 9.5, "Currency": { "Code": "USD" } }, { "@odata.id": null, "Customer": { "Country": "Netherlands", "ID": "C3" },

"CustomerCountryAverage": 5, "Currency": { "Code": "EUR" } }, { "@odata.id": null, "Customer": { "Country": "Netherlands" }, "CustomerCountryAverage": 5, "Currency": { "Code": "EUR" } }, { "@odata.id": null, "CustomerCountryAverage": 9.5, "Currency": { "Code": "USD" } }, { "@odata.id": null, "CustomerCountryAverage": 5, "Currency": { "Code": "EUR" } } ]}

7.8 Transformation SequencesApplying aggregation first covers the most prominent use cases. The slightly more sophisticated question "how much money is earned with small sales" requires filtering the base set before applying the aggregation. To enable this type of question several transformations can be specified in $apply in the order they are to be applied, separated by a forward slash.

Example 81:

GET ~/Sales?$apply=filter(Amount le 1)/aggregate(Amount with sum as Total)

means "filter first, then aggregate", and results in

{ "@odata.context": "$metadata#Sales(Total)", "value": [ { "@odata.id": null, "Total": 2, ... } ]}

Using filter within $apply does not preclude using it as a normal system query option.

Example 82:

GET ~/Sales?$apply=filter(Amount le 2)/groupby((Product/Name), aggregate(Amount with sum as Total)) &$filter=Total ge 4

results in

{ "@odata.context": "$metadata#Sales(Product(Name),Total)", "value": [ { "@odata.id": null, "Total": 4, "Product": { "Name": "Paper" }, ... }, { "@odata.id": null, "Total": 4, "Product": { "Name": "Sugar" }, ... } ]}

odata-data-aggregation-ext-v4.0-wd05 Working Draft 05 21 June 2016Standards Track Draft Copyright © OASIS Open 2016. All Rights Reserved. Page 56 of 61

Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 05/19/20,
ODATA-1137
Page 57:  · Web viewOpen Data (OData) services expose a data model that describes the schema of the service in terms of the Entity Data Model (EDM, see ) and then allows for querying data

Example 83: Revisiting the Example 14 in section for using the from keyword with the aggregate function, the request

GET ~/Sales?$apply=aggregate(Amount as DailyAverage from Time with average as DailyAverage)

could be rewritten in a more procedural way using a transformation sequence returning the same result

GET ~/Sales?$apply=groupby((Time),aggregate(Amount with sum as Total)) /aggregate(Total with average as DailyAverage)

For further examples, consider another data model containing entity sets for cities, countries and continents and the obvious associations between them.

Example 84: getting the population per country with

GET ~/Cities?$apply=groupby((Continent/Name,Country/Name), aggregate(Population with sum as TotalPopulation))

results in

{ "@odata.context": "$metadata#Cities(Continent(Name),Country(Name),TotalPopulation)", "value": [ { "@odata.id": null, "Continent": { "Name": "Asia" }, "Country": { "Name": "China" }, "TotalPopulation": 692.580.000, ... }, { "@odata.id": null, "Continent": { "Name": "Asia" }, "Country": { "Name": "India" }, "TotalPopulation": 390.600.000, ... }, ... ]}

Example 85: all countries with megacities and their continents

GET ~/Cities?$apply=filter(Population ge 10000000) /groupby((Continent/Name,Country/Name), aggregate(Population with sum as TotalPopulation))

Example 86: all countries with tens of millions of city dwellers and the continents only for these countries

GET ~/Cities?$apply=groupby((Continent/Name,Country/Name), aggregate(Population with sum as CountryPopulation)) /filter(CountryPopulation ge 10000000) /concat(identity, groupby((Continent/Name), aggregate(CountryPopulation with sum as TotalPopulation)))

– OR –

GET ~/Cities?$apply=groupby((Continent/Name,Country/Name), aggregate(Population with sum as CountryPopulation)) /filter(CountryPopulation ge 10000000) /groupby((rollup(Continent/Name,Country/Name)), aggregate(CountryPopulation with sum as TotalPopulation))

odata-data-aggregation-ext-v4.0-wd05 Working Draft 05 21 June 2016Standards Track Draft Copyright © OASIS Open 2016. All Rights Reserved. Page 57 of 61

Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 05/19/20,
ODATA-1137
Krause, Gerald, 05/20/20,
ODATA-1201
Page 58:  · Web viewOpen Data (OData) services expose a data model that describes the schema of the service in terms of the Entity Data Model (EDM, see ) and then allows for querying data

Example 87: all countries with tens of millions of city dwellers and all continents with cities independent of their size

GET ~/Cities?$apply=groupby((Continent/Name,Country/Name), aggregate(Population with sum as CountryPopulation)) /concat(filter(CountryPopulation ge 10000000), groupby((Continent/Name), aggregate(CountryPopulation with sum as TotalPopulation)))

Example 88: assuming the data model includes a sales order entity set with related sets for order items and customers, the base set as well as the related items can be filtered before aggregation

GET ~/SalesOrders?$apply=filter(Status eq 'incomplete') /expand(Items,filter(not Shipped)) /groupby((Customer/Country), aggregate(Items/Amount with sum as ItemAmount))

odata-data-aggregation-ext-v4.0-wd05 Working Draft 05 21 June 2016Standards Track Draft Copyright © OASIS Open 2016. All Rights Reserved. Page 58 of 61

Page 59:  · Web viewOpen Data (OData) services expose a data model that describes the schema of the service in terms of the Entity Data Model (EDM, see ) and then allows for querying data

8 ConformanceConforming services MUST follow all rules of this specification for the set transformations and aggregation methods they support. They MUST implement all set transformations and aggregation methods they advertise via the annotation ApplySupported.

Conforming clients MUST be prepared to consume a model that uses any or all of the constructs defined in this specification, including custom aggregation methods defined by the service, and MUST ignore any constructs not defined in this version of the specification.

odata-data-aggregation-ext-v4.0-wd05 Working Draft 05 21 June 2016Standards Track Draft Copyright © OASIS Open 2016. All Rights Reserved. Page 59 of 61

Krause, Gerald, 09/17/18,
ODATA-1073
Page 60:  · Web viewOpen Data (OData) services expose a data model that describes the schema of the service in terms of the Entity Data Model (EDM, see ) and then allows for querying data

Appendix A. AcknowledgmentsThe contributions of the OASIS OData Technical Committee members, enumerated in [OData-Protocol], are gratefully acknowledged.

odata-data-aggregation-ext-v4.0-wd05 Working Draft 05 21 June 2016Standards Track Draft Copyright © OASIS Open 2016. All Rights Reserved. Page 60 of 61

Page 61:  · Web viewOpen Data (OData) services expose a data model that describes the schema of the service in terms of the Entity Data Model (EDM, see ) and then allows for querying data

Appendix B. Revision HistoryRevision Date Editor Changes Made

Working Draft 01

2012-11-12 Ralf Handl Translated contribution into OASIS format

Committee Specification Draft 01

2013-07-25 Ralf HandlHubert HeijkersGerald KrauseMichael PizzoMartin Zurmuehl

Switched to pipe-and-filter-style query language based on composable set transformationsFleshed out examples and addressed numerous editorial and technical issues processed through the TCAdded Conformance section

Committee Specification Draft 02

2014-01-09 Ralf HandlHubert HeijkersGerald KrauseMichael PizzoMartin Zurmuehl

Dynamic properties used all aggregated values. either via aliases or via custom aggregatesRefactored annotations

Committee Specification Draft 03

2015-07-16 Ralf HandlHubert HeijkersGerald KrauseMichael PizzoMartin Zurmuehl

Added compute transformation

Minor clean-up

Committee Specification Draft 03

Ralf HandlHubert HeijkersGerald KrauseMichael PizzoMartin Zurmuehl

odata-data-aggregation-ext-v4.0-wd05 Working Draft 05 21 June 2016Standards Track Draft Copyright © OASIS Open 2016. All Rights Reserved. Page 61 of 61

Handl, Ralf, 08.09.17,
Fill out