问题描述
数据集包含具有以下键的记录:用户ID,期间和点.
The Dataset contains records with following keys: userID, period and points.
是否可以每个period
并按 max points
来查询一条记录.
Is it possible to query one record per period
and by 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},
]
查询结果:
这些是所需的查询结果. 每个时期一个记录和该时期最高分
These are the desired query results. One record per each period and max points for that period
{ userID: 1, period: '2016-01-02', points: 300},
{ userID: 1, period: '2016-01-03', points: 70},
{ userID: 1, period: '2016-01-04', points: 80},
推荐答案
使用用于数据聚合版本4.0的OData扩展,假设该实体设置为查询的名称为PointHistory
:
This is possible with the $apply
query option defined in the OData Extension for Data Aggregation Version 4.0, assuming the entity set to be queried is named PointHistory
:
GET PointHistory?$apply=groupby((period),topcount(1,points))
这将按period
对记录进行分组,按points
对每组中的记录进行排序,然后返回每组中最上面的记录.
This will group the records by period
, order the records in each group by points
and then return the topmost record per group.
这篇关于OData按最大值和字段过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!