Search code examples
odataodata-v4

Syntax for Aggregating a Filtered Dataset in OData v4 Spec using $apply


Suppose I am querying a dataset called Sales with the following schema:

id (int) price (decimal) active (bit)

Using the $apply query option and the aggregate transformation of the OData v4 spec, what would be the syntax for finding the average price for only active Sales?

$apply supports both aggregation and filter transformations, but I can't figure out how to combine them or if you're even allowed to do so.

Thanks!


Solution

  • $apply=filter(Status eq 1)/aggregate(price with average as averagePrice)
    

    maybe you can refer the existing test:

    https://github.com/OData/WebApi/blob/master/OData/test/E2ETest/WebStack.QA.Test.OData/Aggregation/AggregationTests.cs

    https://github.com/OData/WebApi/blob/master/OData/test/UnitTest/System.Web.OData.Test/OData/Query/ApplyQueryOptionTest.cs