1

由于我是 Core-Data 的新手,我希望以 Core-Data 表达式的形式拥有相当于以下 SQL 查询的功能。

SELECT Date,SUM(Amount) from Transactions GROUP BY SUBSTRING(Date,0,7);

我编写了以下代码,这些代码与我之前描述的大致相同,除了group by switch的子字符串部分。

NSExpression *keyPathExpression = [NSExpression expressionForKeyPath:@"amount"];
NSExpression *sumOfAmountExpression = [NSExpression expressionForFunction:@"sum:" arguments:[NSArray arrayWithObject:keyPathExpression]];

NSExpressionDescription *sumOfAmountField = [[NSExpressionDescription alloc] init];
[sumOfAmountField setName:@"sumOfAmount"];
[sumOfAmountField setExpression:sumOfAmountExpression];
[sumOfAmountField setExpressionResultType:NSDoubleAttributeType];

[request setPropertiesToGroupBy:[NSArray arrayWithObjects:@"date", nil]];
[request setResultType:NSDictionaryResultType];
[request setPropertiesToFetch:[NSArray arrayWithObjects:sumOfAmountField, @"date", nil]];

有谁知道如何编辑代码以获得与 SQL 查询相似的结果?

4

0 回答 0