我应该如何通过第一个属性获取数据并将第二个属性的值相加?
通过简单的获取,我有:
但我只需要在 1 行中显示相同的ZINGREDIENT 并将 ZCOUNT的值相加:
它像是:
select ZINGREDIENT, sum(ZCOUNT)
from Table
group by ZINGREDIENT
我应该如何通过第一个属性获取数据并将第二个属性的值相加?
通过简单的获取,我有:
但我只需要在 1 行中显示相同的ZINGREDIENT 并将 ZCOUNT的值相加:
它像是:
select ZINGREDIENT, sum(ZCOUNT)
from Table
group by ZINGREDIENT
这是非常简单的 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];