DB2 Version 9.7 for Linux, UNIX, and Windows

XMLGROUP aggregate function

The XMLGROUP function returns an XML value with a single XQuery document node containing one top-level element node. This is an aggregate expression that will return a single-rooted XML document from a group of rows where each row is mapped to a row subelement.

Read syntax diagramSkip visual syntax diagram
                .-,-------------------------------------.   
                V                                       |   
>>-XMLGROUP--(----expression--+-----------------------+-+------->
                              '-AS-- qname-identifier-'     

>--+--------------------------------------+--------------------->
   |           .-,----------------------. |   
   |           V               .-ASC--. | |   
   '-ORDER BY----| sort-key |--+------+-+-'   
                               '-DESC-'       

>--+--------------------------------------------+--)-----------><
   |         .--------------------------------. |      
   |         V  (1)    .-ROW-- "row"----.     | |      
   '-OPTION----------+-+-ROW-- row-name-+---+-+-'      
                     | .-ROOT-- "rowset"--. |          
                     +-+-ROOT-- root-name-+-+          
                     '-AS ATTRIBUTES--------'          

Notes:
  1. The same clause must not be specified more than once.

The schema is SYSIBM. The function name cannot be specified as a qualified name.

expression
The content of each generated XML element node (or the value of each generated attribute) is specified by an expression. The data type of expression cannot be a structured type (SQLSTATE 42884). The expression can be any SQL expression. If the expression is not a simple column reference, a qname-identifier must be specified.
AS qname-identifier
Specifies the XML element name or attribute name as an SQL identifier. The qname-identifier must be of the form of an XML qualified name, or QName (SQLSTATE 42634). See the W3C XML namespace specifications for more details on valid names. If the name is qualified, the namespace prefix must be declared within the scope (SQLSTATE 42635). If qname-identifier is not specified, expression must be a column name (SQLSTATE 42703). The element name or attribute name is created from the column name using the fully escaped mapping from a column name to an QName.
OPTION
Specifies additional options for constructing the XML value. If no OPTION clause is specified, the default behavior applies.
ROW row-name
Specifies the name of the element to which each row is mapped. If this option is not specified, the default element name is "row".
ROOT root-name
Specifies the name of the root element node. If this option is not specified, the default root element name is "rowset"
AS ATTRIBUTES
Specifies that each expression is mapped to an attribute value with column name or qname-identifier serving as the attribute name.
ORDER BY
Specifies the order of the rows from the same grouping set that are processed in the aggregation. If the ORDER BY clause is omitted, or if the ORDER BY clause cannot distinguish the order of the column data, the rows in the same grouping set are arbitrarily ordered.
sort-key
The sort key can be a column name or a sort-key-expression. Note that if the sort key is a constant, it does not refer to the position of the output column (as in the ordinary ORDER BY clause), but it is simply a constant, which implies no sort key.

Rules

Notes

The default behavior defines a simple mapping between a result set and an XML value. Some additional notes about function behavior apply:

Examples

Assume the following table T1 with integer columns C1 and C2 that contain numeric data stored in a relational format.

C1          C2
----------- -----------
          1           2
          -           2
          1           -
          -           -

  4 record(s) selected.