While trying to learn Play Framework I ran across the following problem:
How do I calculate the summary for a field in a Play Framework Model? I would like to do the equivalent to SQLs “SELECT SUM(totalAmount) FROM events where employee_id = 23”.
I could count the summary myself, like my code below, but I’d rather like the SQL-server to do the counting:
public static Double countSaldoForEmployee(Employee e) {
// Get all Events for the employee
List<Event> events = Event.find("byEmployee", e).fetch();
Double sum = 0.0 ;
for(Event event: events) {
if(event.totalAmount != null) {
sum += event.totalAmount;
}
}
return sum;
}
You should be able to do this with a simple native query. Something like the following.