My requirement to get name,eMail,mobileNo,sum(amount) from a table by core data.
If I go for sql then my sql query looks like this:
select name,eMail,mobileNo,sum(amount) amt1 from T1 GROUP BY name,eMail,mobileNo;
Here I am geting the correct result.
The same I have to do with core data.
I can fetch name and SUM(amount) by providing GROUP BY name.I have provided the below code for that.
[request setPropertiesToGroupBy:[NSArray arrayWithObject:statusDesc]];
I don't know how to add multiple attribute in Group by in core data. Can any one help me for doing that.
NSEntityDescription *entityDesc = [NSEntityDescription entityForName:@"Entity1" inManagedObjectContext:context];
NSAttributeDescription* statusDesc = [entityDesc.attributesByName objectForKey:@"name"];
NSExpression *sumExpression = [NSExpression expressionForFunction:@"sum:" arguments:[NSArray arrayWithObject:[NSExpression expressionForKeyPath:@"totalAmount"]]];
NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc]init];
[expressionDescription setName: @"sumTot"];
[expressionDescription setExpression: sumExpression];
[expressionDescription setExpressionResultType:NSDecimalAttributeType];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDesc];
[request setPropertiesToFetch:[NSArray arrayWithObjects:@"name",expressionDescription, nil]];
[request setPropertiesToGroupBy:[NSArray arrayWithObject:statusDesc]];
[request setResultType:NSDictionaryResultType];
NSError *error = nil;
myArray = [context executeFetchRequest:request error:&error];
Thanks for giving direction to solution.
I Got the Solution. It was a silly mistake between arrayWithObject and arrayWithObjects
NSAttributeDescription* statusDesc = [entityDesc.attributesByName objectForKey:@"name"];
NSAttributeDescription* statusDesc1 = [entityDesc.attributesByName objectForKey:@"eMail"];
NSAttributeDescription* statusDesc2 = [entityDesc.attributesByName objectForKey:@"mobileNo"];
After that I passed all descriptions like this.
[request setPropertiesToGroupBy:[NSArray arrayWithObjects :statusDesc,statusDesc1, statusDesc2, nil]];