Skip to content

Latest commit

 

History

History
65 lines (44 loc) · 2.39 KB

aggregate-functions-avg.md

File metadata and controls

65 lines (44 loc) · 2.39 KB
title description author ms.author ms.date ms.service ms.subservice ms.topic helpviewer_keywords dev_langs
avg Function (XQuery)
Learn about the XQuery function avg() that returns the average of a specified sequence of numbers.
rothja
jroth
08/09/2016
sql
xml
reference
fn:avg function
avg function [XQuery]
XML

Aggregate Functions - avg

[!INCLUDEsqlserver]

Returns the average of a sequence of numbers.

Syntax

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

Arguments

$arg
The sequence of atomic values whose average is computed.

Remarks

All the types of the atomized values that are passed to avg() have to be a subtype of exactly one of the three built-in numeric base types or xdt:untypedAtomic. They cannot be a mixture. Values of type xdt:untypedAtomic are treated as xs:double. The result of avg() receives the base type of the passed in types, such as xs:double in the case of xdt:untypedAtomic.

If the input is statically empty, empty is implied and a static error is raised.

The avg() function returns the average of the numbers computed. For example:

sum( $arg ) div count( $arg )

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

If an xdt:untypedAtomic value cannot be cast to xs:double, the value is disregarded in the input sequence, $arg.

In all other cases, the function returns a static error.

Examples

This topic provides XQuery examples against XML instances that are stored in various xml type columns in the AdventureWorks database.

A. Using the avg() XQuery function to find work center locations in the manufacturing process in which labor hours are greater than the average for all work center locations.

You can rewrite the query provided in min function (XQuery) to use the avg() function.

Implementation Limitations

These are the limitations:

  • The avg() function maps all integers to xs:decimal.

  • The avg() function on values of type xs:duration is not supported.

  • Sequences that mix types across base type boundaries are not supported.

See Also

XQuery Functions against the xml Data Type