Back
sum()
Calculates the sum of values for a given property.
Uses the SQL function SUM
.
If no records can be found to perform the calculation on you can use the ifNull
argument to decide what should be returned.
// Get the sum of all salaries
allSalaries = model("employee").sum("salary");
// Get the sum of all salaries for employees in a given country
allAustralianSalaries = model("employee").sum(property="salary", include="country", where="countryname='Australia'");
// Make sure a numeric value is always returned, even if there are no records analyzed by the query
salarySum = model("employee").sum(property="salary", where="salary BETWEEN #params.min# AND #params.max#", ifNull=0);
Copied!