I have a table (Transactions) which holds records containing the Account_name and an Amount for the transaction. I would like to calculate the total for all transactions per
Bear in mind that CoreData is not a relational database, so you should think of entities not "tables", and objects not "records". Note also that by convention, attribute names should not begin with Uppercase letters. That said, it is possible to construct a fetch to achieve what you want. The key steps are:
NSPredicate
to filter according to your chosen criteriaresultType
to .DictionaryResultType
(required for "Group By")NSExpression
and associated NSExpressionDescription
).NSSortDescriptor
to order by name, descendingSee the following code:
let fetch = NSFetchRequest(entityName: "Transaction")
let predicate = NSPredicate(format: "Account_name like %@ AND Amount > %@", "Private*",NSNumber(double: 1000.0))
fetch.predicate = predicate
fetch.resultType = .DictionaryResultType
let sumExpression = NSExpression(format: "sum:(Amount)")
let sumED = NSExpressionDescription()
sumED.expression = sumExpression
sumED.name = "sumOfAmount"
sumED.expressionResultType = .DoubleAttributeType
fetch.propertiesToFetch = ["Account_name", sumED]
fetch.propertiesToGroupBy = ["Account_name"]
let sort = NSSortDescriptor(key: "Account_name", ascending: false)
fetch.sortDescriptors = [sort]
let results = managedObjectContext?.executeFetchRequest(fetch, error: nil) as NSArray?
The result will be an array of dictionaries, with keys "Account_name" and "sumOfAmount".
EDIT To extract the value for a particular Account_name, use another predicate:
let newPredicate = NSPredicate(format: "Account_name like %@", "toto")!
if let resultsArray = results { // to unwrap the optional
let filteredResults = resultsArray.filteredArrayUsingPredicate(newPredicate)
let sumOfAmount = (filteredResults[0] as NSDictionary)["sumOfAmount"] as Double
}
I've ignored the fact that the previous example filtered Account_name
to those beginning "Private", and assumed you will have one (and only one) account with name "toto" (hence filteredResults[0]
); your production code should check.