OData按最大值和字段过滤

问题描述 投票:2回答:1

数据集包含具有以下键的记录:userID,period和points。

是否可以按period和max points查询一条记录。

示例场景

数据集

[
 /* period: 2016-01-01 */
 { userID: 1, period: '2016-01-01', points: 100},
 { userID: 1, period: '2016-01-01', points: 200},
 { userID: 1, period: '2016-01-01', points: 300},

 /* period: 2016-01-02 */
 { userID: 1, period: '2016-01-02', points: 50},
 { userID: 1, period: '2016-01-02', points: 70},
 { userID: 1, period: '2016-01-02', points: 10},

 /* period: 2016-01-03 */
 { userID: 1, period: '2016-01-03', points: 80},
 { userID: 1, period: '2016-01-03', points: 20},
 { userID: 1, period: '2016-01-03', points: 0},
]

查询结果:

这些是所需的查询结果。每个期间一个记录和该期间的最大点数

 { userID: 1, period: '2016-01-02', points: 300},
 { userID: 1, period: '2016-01-03', points: 70},
 { userID: 1, period: '2016-01-04', points: 80},
odata
1个回答
1
投票

这可以使用$apply中定义的OData Extension for Data Aggregation Version 4.0查询选项,假设要查询的实体名为PointHistory

GET PointHistory?$apply=groupby((period),topcount(1,points))

这将按period对记录进行分组,按points对每组中的记录进行排序,然后返回每组最高记录。

© www.soinside.com 2019 - 2024. All rights reserved.