I know how to write the request I need in SQL but can’t find a way to implement it using Core Data, or if I find a solution it would be ugly and messy, using 20+ lines of code…
My request is SELECT MAX(distance) FROM DataTrip WHERE idTrip = (SELECT DISTINCT(idTrip) FROM DataTrip);
Any idea how I can do that using Core Data with NSSortDescriptor, NSPredicate, NSExpressionDescription, etc.?
Thank you!
Update 1
Data model for DataTrip object:
@interface DataTrip : NSManagedObject {
@private
}
@property (nonatomic, retain) NSNumber * speed;
@property (nonatomic, retain) NSNumber * heading;
@property (nonatomic, retain) NSNumber * longitude;
@property (nonatomic, retain) NSNumber * timestamp;
@property (nonatomic, retain) NSNumber * latitude;
// More properties here
@property (nonatomic, retain) NSNumber * idTrip;
@property (nonatomic, retain) NSNumber * distance;
@end
So I would have in my database multiple rows with the same idTrip, and I want the max value for distance for each idTrip.
The question is difficult to answer because it hinges on the assumption that SQL and Core Data have anything to do with each other. They in fact, have very little to do with each other. Core Data is not a database and should not be thought of as or treated as one. The way you would accomplish the task you specified in the question is highly dependent on the data model that you have created. Without that information it’s not possible to give any more helpful information.