Querying max, min and other via Core Data

3 Solutions Collect From Internet About “Querying max, min and other via Core Data”

You can indeed have an NSFetchRequest calculate such values. You need to set your fetch request to return NSDictionaryResultType results, and create appropriate NSExpressionDescription objects to describe the operations (e.g. min, max, sum, etc.) you’re after.

Take a look at the worked example in Apple’s Core Data Programming Guide: http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CoreData/Articles/cdFetching.html#//apple_ref/doc/uid/TP40002484-SW6 .

You can use query builder, which allows you write your requests like this:

NSDictionary *productTotalSumAndAveragePriceGroupedByCountries = 
[[[[[Product all
] aggregatedBy:@[
                 @[kAggregateSum, @"amount"],
                 @[kAggregatorAverage, @"price"]]
] groupedBy:@[@"country"]
] having:predicate
] execute];