1

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

Rocker
  • 1,269
  • 7
  • 15
  • Is status a number or text? What values can it take? – pbasdf Dec 16 '14 at 08:50
  • Set predicates for your conditions (for example status=0 and user=user1 and anything else). For getting output with grouped by distinct locations, you can use `setPropertiesToGroupBy`. Play with it a little. Can also look at `setPropertiesToFetch`. – Zen Dec 16 '14 at 09:22
  • @pdasdf status is a number. value can be between 0 to 5 – Rocker Dec 16 '14 at 17:33

1 Answers1

4

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);
Balazs Nemeth
  • 2,333
  • 19
  • 29
  • Status can be between 0 to 5 but i am only interested in status 0 – Rocker Dec 16 '14 at 17:04
  • At this case, I think CD is not able to handle it in one single fetchrequest... Unfortunately you have to get the location, locationCount as I poseted it before, than you have to iterate through the resultes array, and perform a new NSFetchRequest where the predicate is representing the status == 0 condition... I know, it is a lot of computation effort, but the NSExpression is not able to handle separated predicates... – Balazs Nemeth Dec 20 '14 at 19:05