avg function
The fn:avg function returns the average of the values in a sequence.
Syntax
- sequence-expression
- A sequence that contains items of any of the following atomic
types, or an empty sequence:
- xs:float
- xs:double
- xs:decimal
- xs:integer
- xdt:untypedAtomic
- xdt:dayTimeDuration
- xdt:yearMonthDuration
- A type that is derived from any of the previously listed types
Input items of type xdt:untypedAtomic are cast to xs:double. After this casting, all of the items in the input sequence must be convertible to a common type by promotion or subtype substitution. The average is computed in this common type. For example, if the input sequence contains items of type money (derived from xs:decimal) and stockprice (derived from xs:float), the average is computed in the type xs:float.
Returned value
If sequence-expression is not the empty sequence, the returned value is the average of the values in sequence-expression. The data type of the returned value is the same as the data type of the items in sequence-expression, or the data type to which the items in sequence-expression are promoted.
If sequence-expression is the empty sequence, the empty sequence is returned.
Example
The following function returns the average of the sequence (5, 1.0E2, 40.5):
fn:avg((5, 1.0E2, 40.5))The values are promoted to the xs:double data type. The function returns the xs:double value 4.85E1, which is serialized as "48.5".
