Description
The fn:avg function averages a sequence of values. The $arg sequence can contain a mixture of numeric and untyped values. Numeric values are promoted as necessary to make them all the same type. Untyped values are cast as numeric xs:double values.
The function can also be used on duration values, so the $arg sequence can contain all xs:yearMonthDuration values or all xs:dayTimeDuration values (but not a mixture of the two).
Special care should be taken with any "missing" values when using the fn:avg function. See functx:avg-empty-is-zero for more information.
This description is © Copyright 2007, Priscilla Walmsley. It is excerpted from the book XQuery by Priscilla Walmsley, O'Reilly, 2007. For a complete explanation of this function, please refer to Appendix A of the book. Arguments and Return TypeName | Type |
$arg |
xs:anyAtomicType* |
return value |
xs:anyAtomicType? |
Exampleslet $ordDoc := | doc('https://www.datypic.com/functx./input/order.xml') | return |
XQuery Example | Results |
---|
avg( (1, 2, 3, 4, 5) ) |
3 |
avg( (1, 2, 3, (), 4, 5) ) |
3 |
avg(
(xs:yearMonthDuration('P4M'),
xs:yearMonthDuration('P6M') ) ) |
P5M |
avg($ordDoc//item/@quantity) |
1.1666666666666667 |
avg( () ) |
() |
avg($ordDoc//item/@dept) |
Error FORG0001 |
See AlsoHistory |
Recommended Reading:
|