The entitySelection.average( ) method returns the arithmetic mean (average) of all the non-null values of attributePath in the entity selection.
Pass in the attributePath parameter the attribute path to evaluate.
Only numerical values are taken into account for the calculation. Note however that, if the attributePath of the entity selection contains mixed value types, entitySelection.average( ) takes all scalar elements into account to calculate the average value.
Note: Date values are converted to numerical values (seconds) and used to calculate the average.
entitySelection.average( ) returns undefined if the entity selection is empty or attributePath does not contain numerical values.
An error is returned if:
- attributePath is a related attribute,
- attributePath designates an attribute that does not exist in the entity selection dataclass.
We want to obtain a list of employees whose salary is higher than the average salary:
C_REAL($averageSalary)
C_OBJECT($moreThanAv)
$averageSalary:=ds.Employee.all().average("salary")
$moreThanAv:=ds.Employee.query("salary > :1";$averageSalary)