Skip to content

Miscellaneous

average()

average() — returns any

Calculates the average value for a given property. Uses the SQL function AVG. If no records can be found to perform the calculation on you can use the ifNull argument to decide what should be returned.

NameTypeRequiredDefaultDescription
propertystringyesName of the property to calculate the average for.
wherestringyesSee documentation for findAll.
includestringyesSee documentation for findAll.
distinctbooleanyesfalseWhen true, AVG will be performed only on each unique instance of a value, regardless of how many times the value occurs.
parameterizeanyyestrueSee documentation for findAll.
ifNullanyyesThe value returned if no records are found. Common usage is to set this to 0 to make sure a numeric value is always returned instead of a blank string.
includeSoftDeletesbooleanyesfalseSee documentation for findAll.
groupstringyesSee documentation for findAll.
// Get the average salary for all employees
avgSalary = model("employee").average("salary");

// Get the average salary for employees in a given department
avgSalary = model("employee").average(property="salary", where="departmentId=##params.key##");

// Make sure a numeric value is always returned if no records are calculated
avgSalary = model("employee").average(property="salary", where="salary BETWEEN ##params.min## AND ##params.max##", ifNull=0);