1

我应该如何通过第一个属性获取数据并将第二个属性的值相加?

通过简单的获取,我有:

在此处输入图像描述

但我只需要在 1 行中显示相同的ZINGREDIENT 并将 ZCOUNT的值相加

在此处输入图像描述

它像是:

select ZINGREDIENT, sum(ZCOUNT) 
from Table
group by ZINGREDIENT
4

1 回答 1

2

这是非常简单的 NSFetchRequest 版本:

    NSManagedObjectContext* context = [object managedObjectContext];
    NSFetchRequest* request = [NSFetchRequest fetchRequestWithEntityName:@"Join"];
    [request setResultType:NSDictionaryResultType];
    [request setReturnsDistinctResults:YES];
    NSExpression *keyPathExpression = [NSExpression expressionForKeyPath:@"count"];
    NSExpression* expr = [NSExpression expressionForFunction:@"sum:" arguments:@[keyPathExpression]];
    NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init];
    [expressionDescription setName:@"count"];
    [expressionDescription setExpression:expr];
    [expressionDescription setExpressionResultType:NSInteger32AttributeType];
    [request setPropertiesToGroupBy:@[@"ingredient"]];
    [request setPropertiesToFetch:@[@"ingredient",expressionDescription]];
    NSError* error = nil;
    NSArray* rez = [context executeFetchRequest:request error:&error];
于 2013-03-05T06:26:05.123 回答