核心数据组按月和总和 [英] Core Data Group By Monthly and Sum

查看:115
本文介绍了核心数据组按月和总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法根据月份 GROUP BY 核心数据,然后 SUM 它的属性?
我有这样的数据

  ----------------- ------- 
date |金额
------------------------
30-08-2017 | 124000000
28-10-2017 | 124000000
16-10-2017 | 124000000
14-12-2017 | 124000000
20-12-2017 | 124000000
------------------------

我只需要显示一个最近的年份,所以我创建了像这样的获取请求

  NSDate *现在= [NSDate日期]; 
NSDate * oneYearAgo = [now addYears:-1];

NSEntityDescription * entity = [NSEntityDescription entityForName:@MyTableinManagedObjectContext:moc];
[request setEntity:entity];

NSPredicate * predicate = [NSPredicate predicateWithFormat:@(myDate> =%@)AND(myDate< =%@)AND isActive ==%@,oneYearAgo,now,@YES] ;
[request setPredicate:predicate];

NSSortDescriptor * sort = [[NSSortDescriptor alloc] initWithKey:@myDate升序:YES];
[request setSortDescriptors:@ [sort]];

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

[request setPropertiesToFetch:[NSArray arrayWithObjects:@myDate,ex,nil]];
[request setPropertiesToGroupBy:[NSArray arrayWithObject:@myDate]];
[request setResultType:NSDictionaryResultType];

NSArray * result = [moc executeFetchRequest:request error:nil];

这是我所需要的最接近的解决方案,但该分组仍然基于日期而不是月份,不要总结它的金额。



所以我想得到这样的结果

  2017年8月| 124000000 
2017年10月| 248000000
2017年12月| 248000000

任何帮助将不胜感激。谢谢!

解决方案

您可以使用带自定义节路径的NSFetchResultsContoller根据月份对数据进行分组。然后,您可以对该部分中的所有项目进行求和以获得所需的结果。以下代码片段可用于创建获取结果控制器:

pre $ NSFetchedResultsController * dateAmountFetchResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest: fetchRequest
managedObjectContext:managedObjectContext
sectionNameKeyPath:@monthIndexcacheName:@MyEntity];

在DateAmount实体上创建一个名为monthIndex的新的transient属性。然后在DateAmount类中创建一个方法 - (NSString *)monthIndex,以返回正确的月份数字符串。根据上述代码片段,应使用@monthIndex作为获取结果控制器的部分名称键路径。
在获取请求的排序描述符中,使用排序描述符和键值@monthIndex对项目进行排序,因为这是NSFetchedResultsController的要求。


Is there any way to GROUP BY core data based on its month then SUM its properties? I have data like this

------------------------
   date    |   amount  
------------------------ 
30-08-2017 |  124000000
28-10-2017 |  124000000
16-10-2017 |  124000000
14-12-2017 |  124000000
20-12-2017 |  124000000
------------------------

I just need to show one latest year, so I created my fetch request like this

NSDate *now = [NSDate date];
NSDate *oneYearAgo = [now addYears:-1];

NSEntityDescription *entity = [NSEntityDescription entityForName:@"MyTable" inManagedObjectContext:moc];
[request setEntity:entity];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(myDate >= %@) AND (myDate <= %@) AND isActive == %@", oneYearAgo, now, @YES];
[request setPredicate:predicate];

NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"myDate" ascending:YES];
[request setSortDescriptors:@[sort]];

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

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

NSArray *result = [moc executeFetchRequest:request error:nil];

That's closest solution for what I need, but that grouping still based on day but month, and it doesn't sum its amount.

So I want to get the result like this

Aug 2017 | 124000000
Oct 2017 | 248000000
Dec 2017 | 248000000

Any help would be appreciated. Thank you!

解决方案

You can use the NSFetchResultsContoller with custom section path to group your data based upon the month. Then you can sum all the items in the section to get the required results. Following code snippet can be used to create fetch result controller

NSFetchedResultsController *dateAmountFetchResultsController =
    [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                        managedObjectContext:managedObjectContext
                                    sectionNameKeyPath:@"monthIndex" cacheName:@"MyEntity"];

Create a new transient attribute on your DateAmount entity, called "monthIndex". Then create a method on your DateAmount class -(NSString*)monthIndex, to return the correct month number string. @"monthIndex" should be used as the section name key path for your fetched results controller as per above code snippet. In the sort descriptor of the fetch request, use sort descriptor with key @"monthIndex" for sorting the items, as it is a requirement of the NSFetchedResultsController.

这篇关于核心数据组按月和总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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