简体   繁体   English

coredata-获取具有最大值的不同行

[英]coredata - fetch distinct rows having maximum value

I'm trying to setup my NSFetchRequest to core data to retrieve the rows "unique name which have greater rate" 我正在尝试将我的NSFetchRequest设置为核心数据,以检索“具有较高比率的唯一名称”行

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"rate = max(rate)"];
[request setPropertiesToFetch:@[@"name"]];
request.predicate = predicate;
[request setReturnsDistinctResults:YES];

But the above return only one row which have maximum rate. 但以上仅返回具有最大速率的一行。

required sample 所需样品

  name | rate | factor |
_______|______|________|
John   |  3.2 |    7   |
Betty  |  5.5 |    7   |
Betty  |  7.1 |    2   |
Betty  |  3.1 |    2   |
Edward |  5.5 |    1   |
Edward |  4.5 |    2   |
John   |  4.3 |    4   |

How would i set up the request to return an array like 我将如何设置返回数组的请求

John,  4.3, 4
Betty, 7.1, 2
Edward,5.5, 1

And can we sort it (sort by rate desc) with the fetch query itself ? 我们是否可以使用获取查询本身对其进行排序(按速率降序排序)? So the result array will be 所以结果数组将是

Betty, 7.1, 2
Edward,5.5, 1
John,  4.3, 4

Set the propertiesToGroupBy property of your NSFetchRequest to include "name". 将您的NSFetchRequestpropertiesToGroupBy属性设置为包括“名称”。

Note: you may have to aggregate the factor column in some way as well, for the group by to work. 注意:为了使分组依据起作用,您可能还必须以某种方式汇总因子列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM