このクエリを NSManagedObjects で複製しようとしています:
SELECT
t1.znumber, t2.znumber, AVG(t1.zaroma)
FROM
zcuppings t1
LEFT JOIN zrounds t2 ON (t1.zround=t2.z_pk)
LEFT JOIN zexaminees t3 ON (t2.zexaminee=t3.z_pk)
WHERE
t3.zcourse = 1
GROUP BY
t1.znumber, t2.znumber
このような:
NSFetchRequest* fetch = [NSFetchRequest fetchRequestWithEntityName:@"Cuppings"];
fetch.predicate = [NSPredicate predicateWithFormat:@"round.examinee.course == %i",1];
NSEntityDescription* entity = [NSEntityDescription entityForName:@"Cuppings"
inManagedObjectContext:managedObjectContext];
NSAttributeDescription* group = [entity.attributesByName objectForKey:@"number"];
NSAttributeDescription* group2 = [entity.attributesByName objectForKey:@"round.number"];
NSExpression *keyPathExpression = [NSExpression expressionForKeyPath: @"aroma"];
NSExpression *exprAVG = [NSExpression expressionForFunction: @"average:"
arguments: [NSArray arrayWithObject:keyPathExpression]];
NSExpressionDescription *exprDescription = [[NSExpressionDescription alloc] init];
[exprDescription setName: @"avgAroma"];
[exprDescription setExpression: exprAVG];
[exprDescription setExpressionResultType:NSDecimalAttributeType];
[fetch setPropertiesToFetch:[NSArray arrayWithObjects:group, group2,
exprDescription, nil]];
[fetch setPropertiesToGroupBy:[NSArray arrayWithObjects:group, group2, nil]];
[fetch setResultType:NSDictionaryResultType];
NSError* error = nil;
NSArray *results = [managedObjectContext executeFetchRequest:fetch error:&error];
NSLog(@"%@", results);
しかし、group2 は nil に等しく、「number」フィールドしか得られません...何が間違っていますか?