OData filter by max value and field

久未见 提交于 2020-01-04 05:34:25

问题


The Dataset contains records with following keys: userID, period and points.

Is it possible to query one record per period and by max points.

Example scenario

Dataset

[
 /* 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},
]

Query results:

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},

回答1:


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))

This will group the records by period, order the records in each group by points and then return the topmost record per group.



来源:https://stackoverflow.com/questions/49084141/odata-filter-by-max-value-and-field

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!