I'm trying to get the average value of an attribute in a child entity while also trying to only include a select set of records.
I have two entities in my Core Data model: Invoice and InvoiceDetail.
Invoice:<br>
invoiceNum - attribute<br>
invoiceDate - attribute<br>
invoiceDetails - one-to-many relationship to InvoiceDetail
InvoiceDetail:<br>
itemAmount - attribute<br>
itemType - attribute<br>
invoice - one-to-one relationship to Invoice<br>
If I wanted to just get the average value of itemAmount for an entire invoice, I would use the following (invoice is an NSManagedObject):
float avgAmount = [[invoice valueForKeyPath:@"invoiceDetails.@avg.itemAmount"] floatValue];
However, I'm trying to only get the average for objects where itemType = 1. I can loop through the invoiceDetail items and do this manually, but I know that this will cause a performance issue. I'm not sure what is the best way to go about doing this.
Thanks for your help.