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!