Loading TOC...

fn:avg

fn:avg(
   $arg as xs:anyAtomicType*
) as xs:anyAtomicType?

Summary

Returns the average of the values in the input sequence $arg, that is, the sum of the values divided by the number of values.

If $arg is the empty sequence, the empty sequence is returned.

If $arg contains values of type xs:untypedAtomic they are cast to xs:double.

Duration values must either all be xs:yearMonthDuration values or must all be xs:dayTimeDuration values. For numeric values, the numeric promotion rules defined in 6.2 Operators on Numeric Values are used to promote all values to a single common type. After these operations, $arg must contain items of a single type, which must be one of the four numeric types,xs:yearMonthDuration or xs:dayTimeDuration or one if its subtypes.

If the above conditions are not met, then a type error is raised [err:FORG0006].

Otherwise, returns the average of the values computed as sum($arg) div count($arg).

For detailed type semantics, see Section 7.2.10 The fn:min, fn:max, fn:avg, and fn:sum functions[FS].

Parameters
$arg The sequence of values to average.

Example

Assume:
$d1 = xs:yearMonthDuration("P20Y")
$d2 = xs:yearMonthDuration("P10M")
$seq3 = (3, 4, 5)

Then:

fn:avg($seq3) returns 4.0.

fn:avg(($d1, $d2))
returns a yearMonthDuration with value 125 months.

fn:avg(($d1, $seq3)) raises a type error [err:FORG0006].

fn:avg(()) returns ().

fn:avg((xs:float('INF')), xs:float('-INF')) returns NaN.

fn:avg(($seq3, xs:float('NaN')) returns NaN.

Stack Overflow iconStack Overflow: Get the most useful answers to questions from the MarkLogic community, or ask your own question.