我正在尝试获取核心数据,其中我按我的资产日期的月份进行分组。
在SQL中
SELECT      SUM(total) as value,
            date as date,
            strftime('%m-%Y', date) as convertDate  
FROM        table
GROUP BY    convertDate 我没有看到任何可以使用strftime的函数。有没有一种方法可以用NSFetchRequest代替NSFetchedResultController
发布于 2016-01-23 08:12:25
您可以使用NSExpressionDescription来完成此操作
有关使用NSFetchRequest进行分组的信息,您可以查看this tutorial或此stackOverFlow question
对于此处要添加到NSFetchRequest的NSExpressionDescription的总和
NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init];
expressionDescription.name = @"sumOfAmounts";
expressionDescription.expression = [NSExpression expressionForKeyPath:@"@sum.total"];
expressionDescription.expressionResultType = NSDecimalAttributeType;对于日期,我认为您可以在获取获取记录时使用NSDateFormatter对其进行格式化。
让我知道这是否有帮助:)
更新
[fetch setPropertiesToFetch:[NSArray arrayWithObjects:statusDesc, expressionDescription, nil]];
[fetch setPropertiesToGroupBy:[NSArray arrayWithObject:statusDesc]];
[fetch setResultType:NSDictionaryResultType];
NSError* error = nil;
NSArray *results = [myManagedObjectContext executeFetchRequest:fetch
                                                         error:&error];https://stackoverflow.com/questions/34957217
复制相似问题