Return to v1.4.5 docs

average()



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.

Name Type Required Default Description
property string Yes Name of the property to calculate the average for.
where string No See documentation for findAll.
include string No See documentation for findAll.
distinct boolean No false When true, AVG will be performed only on each unique instance of a value, regardless of how many times the value occurs.
parameterize any No true See documentation for findAll.
ifNull any No The 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.
includeSoftDeletes boolean No false See documentation for findAll.
group string No See 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);