GROUP BY 和 SUM() 使用核心数据 [英] GROUP BY with SUM() using Core Data

查看:18
本文介绍了GROUP BY 和 SUM() 使用核心数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到过类似的问题,但他们都没有完整的示例来说明如何完成它.

I came across similar questions but none of them had a complete example of how to accomplish it.

我试图翻译的 SQL 查询是这样的:

The SQL query I am trying to translate is this:

SELECT date, SUM(amount) FROM Table GROUP BY date;

我需要帮助调试以下片段代码(当前 fetchRequest 返回 nil):

I need help debugging the following piece code (currently the fetchRequest returns nil):

entity = [NSEntityDescription entityForName:@"Table" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];

NSExpressionDescription* ex = [[NSExpressionDescription alloc] init];
[ex setExpression:[NSExpression expressionWithFormat:@"@sum.amount"]];
[ex setExpressionResultType:NSDecimalAttributeType];

[fetchRequest setPropertiesToFetch:[NSArray arrayWithObjects:@"date", ex, nil]];
[fetchRequest setPropertiesToGroupBy:[NSArray arrayWithObject:@"date"]];
[fetchRequest setResultType:NSDictionaryResultType ];

[self.managedObjectContext executeFetchRequest:fetchRequest error:&error];

这是错误:

2012-09-28 13:58:46.319 App[12205:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'

推荐答案

上面的代码需要

[ex setName:@"somename"];

在执行 fetchRequest 之前.

before executing the fetchRequest.

这篇关于GROUP BY 和 SUM() 使用核心数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆