1

最初の属性でデータをフェッチし、2番目の属性の値を合計するにはどうすればよいですか?

単純なフェッチで私は持っています:

ここに画像の説明を入力してください

しかし、同じZINGREDIENTを1行だけに表示し、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 に答える