home contribute faq download

FunctX XQuery Functions

fn:avg

The average of a sequence of values

Google
Webdatypic.com

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 Type

NameType
$arg xs:anyAtomicType*
return value xs:anyAtomicType?

Examples

let $ordDoc :=
doc('https://www.datypic.com/functx./input/order.xml')
return
XQuery ExampleResults
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 Also

functx:avg-empty-is-zeroThe average, counting "empty" values as zero
fn:sumThe sum of values in a sequence
fn:countThe number of items in a sequence

History

Published OnLast UpdatedContributor(s)
2006-06-272007-02-26W3C, XQuery 1.0 and XPath 2.0 Functions and Operators, http://www.w3.org/TR/xpath-functions/
Datypic XQuery Services

Recommended Reading:

XQuery