1

我正在尝试使用 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 而我只得到“数字”字段......我做错了什么?

4

0 回答 0