GROUP BY Equivalent for Master Data

I know that I can use @distinctUnionOfObjects to find something like the following in SQL:

SELECT a_value FROM my_table GROUP BY a_value; 

I'm looking for all the data returned in an array, not just an array of values ​​that matches a group by expression. Essentially, I'm looking for the equivalent of the following SQL query for master data:

 SELECT * FROM my_table GROUP BY a_value; 
+6
sql objective-c iphone core-data
source share
4 answers

It is analog

SELECT 'Status', COUNT(*) FROM 'Records' GROUP BY 'Status' :

 NSFetchRequest* fetch = [NSFetchRequest fetchRequestWithEntityName:@"Record"]; NSEntityDescription* entity = [NSEntityDescription entityForName:@"Record" inManagedObjectContext:myManagedObjectContext]; NSAttributeDescription* statusDesc = [entity.attributesByName objectForKey:@"status"]; NSExpression *keyPathExpression = [NSExpression expressionForKeyPath: @"url"]; // Does not really matter NSExpression *countExpression = [NSExpression expressionForFunction: @"count:" arguments: [NSArray arrayWithObject:keyPathExpression]]; NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init]; [expressionDescription setName: @"count"]; [expressionDescription setExpression: countExpression]; [expressionDescription setExpressionResultType: NSInteger32AttributeType]; [fetch setPropertiesToFetch:[NSArray arrayWithObjects:statusDesc, expressionDescription, nil]]; [fetch setPropertiesToGroupBy:[NSArray arrayWithObject:statusDesc]]; [fetch setResultType:NSDictionaryResultType]; NSError* error = nil; NSArray *results = [myManagedObjectContext executeFetchRequest:fetch error:&error]; 

Found here

+15
source share

You can try using the NSFetchedResultsController object to provide grouping using the sectionNameKeyPath construct in the initializer. Note that FRCs are mainly used to connect to a table view, but this is not necessary. This way you can group the results using your NameKeyPath section, which can also be a transient attribute in your model.

As a comment, I would not recommend thinking about Core Data from a database perspective, but it is not. The master data is built so that it’s easier for you to save and manage the relationships of objects. Just because on iOS it runs on top of SQLite does not make it a database replacement.

Link: NSFRC Help

+3
source share

I find this method (roughly similar to the accepted answer) a little cleaner and clearer. This is the equivalent of SQL:

SELECT COUNT(*), a_value FROM my_table GROUP BY a_value;

 NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:[MyTable className]]; // create expression for grouped column NSExpressionDescription *aValueDescription = [[NSExpressionDescription alloc] init]; aValueDescription.name = @"aValue"; // name of key in result dictionary aValueDescription.expression = [NSExpression expressionWithFormat:@"aValue"]; aValueDescription.expressionResultType = NSObjectIDAttributeType; // create expression for count NSExpressionDescription *countDescription = [[NSExpressionDescription alloc] init]; countDescription.name = @"count"; // name of dictionary key in result dictionary countDescription.expression = [NSExpression expressionWithFormat:@" aValue.@count "]; countDescription.expressionResultType = NSInteger32AttributeType; // fill out fetch request fetchRequest.propertiesToGroupBy = @[@"aValue"]; fetchRequest.propertiesToFetch = @[aValueDescription, countDescription]; //fetchRequest.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"count" ascending:NO]]; // not sure why this crashes fetchRequest.resultType = NSDictionaryResultType; // required for "group by" requests NSError *error = nil; NSArray *results = [managedObjectContext executeFetchRequest:fetchRequest error:&error]; 

The returned results is an array of NSDictionary. Please note: the name description properties can be whatever you want - these are just the key names in the returned dictionaries. You can add a predicate to a query to filter rows from a table; this code returns all rows.

The bonus points to everyone who can tell me how to get the sort descriptor to work ...

0
source share

You can use Predicate Programming .

EDIT: Sorry, you cannot use predicates for Group By, at least not in a straightforward way. I just read the links.

Limitations: you cannot translate "arbitrary" SQL queries into predicate or query queries. For example, it is not possible to convert an SQL statement, for example

SELECT t1.name, V1, V2

 FROM table1 t1 JOIN (SELECT t2.name AS V1, count(*) AS V2 FROM table2 t2 GROUP BY t2.name as V) on t1.name = V.V1 
-one
source share

All Articles