Needed help for complex core data query

188 Views Asked by At

I have table called UserProfile as mention below

enter image description here

I need distinct location, count for distinct location and count for status whose value is 0 at distinct location for user = user1. Status value can be between 0 t0 5(number)

so output will be

abc 3 2
abd 1 1

where abc is unique location, 3 is total count for abc in table and 2 is total count for status =0 at location abc

1

There are 1 best solutions below

2
On

I Can give you some inspiration, due to I assume, that the 'status' can be only 0 or 1. If that's not true, than - I think - you have to execute two separated fetch with different predicated (one for fetch the count of location referring user1 and an other the count of status where user = user1 and status = 1).

NSFetchRequest *fetchRequest =  [[NSFetchRequest alloc] init];

//create entity
NSEntityDescription *entity = [NSEntityDescription entityForName:@"UserProfile" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];

//filter for user = user1
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"user == %@",user1];
[fetchRequest setPredicate:predicate];

//count of the status == 1 
NSExpressionDescription* sumStatusIsOne = [[NSExpressionDescription alloc] init];
[sumStatusIsOne setExpression:[NSExpression expressionForFunction:@"sum:"
                                            arguments:[NSArray arrayWithObject:[NSExpression expressionForKeyPath:@"status"]]]];
[sumStatusIsOne setExpressionResultType:NSDecimalAttributeType];
[sumStatusIsOne setName:@"sumStatusIsOne"];

//count of the location
NSExpressionDescription* locationCount = [[NSExpressionDescription alloc] init];
[locationCount setExpression:[NSExpression expressionForFunction:@"count:"
                                                 arguments:[NSArray arrayWithObject:[NSExpression expressionForKeyPath:@"Location"]]]
 ];
[locationCount setExpressionResultType:NSDecimalAttributeType];
[locationCount setName:@"locationCount"];

NSArray *subtractComponents = [NSArray arrayWithObjects:
                          locationCount.expression,
                          sumStatusIsOne.expression, nil];

//Count of zero status !!!Important, it is working if the 'status' can be only 1 or 0!!!
NSExpression *countOfZeroStatusExpression = [NSExpression expressionForFunction:@"from:subtract:" arguments:subtractComponents];
NSExpressionDescription* countOfZeroStatus = [[NSExpressionDescription alloc] init];
[countOfZeroStatus setExpression:countOfZeroStatusExpression];
[countOfZeroStatus setExpressionResultType:NSDecimalAttributeType];
[countOfZeroStatus setName:@"countOfZeroStatusExpression"];

[fetchRequest setPropertiesToFetch:[NSArray arrayWithObjects:@"Location", locationCount,countOfZeroStatus, nil]];
[fetchRequest setPropertiesToGroupBy:[NSArray arrayWithObject:@"Location"]];
[fetchRequest setResultType:NSDictionaryResultType ];

NSError *error;
NSArray *grouppedResults = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];

NSLog(grouppedResults);