expression
An XML value. The content is escaped unless the data type is XML. The order-by-expression orders the elements returned by the function.
order-by-expression
An expression used to order the XML elements according to the value of this expression.
When an ORDER BY clause contains constants, they are interpreted by the optimizer and then replaced by an equivalent ORDER
BY clause. For example, the optimizer interprets ORDER BY 'a' as ORDER BY expression.
A query block containing more than one aggregate function with valid ORDER BY clauses can be executed if the ORDER BY clauses
can be logically combined into a single ORDER BY clause. For example, the following clauses:
ORDER BY expression1, 'a', expression2
ORDER BY expression1, 'b', expression2, 'c', expression3
Any values that are NULL are omitted from the result. If all inputs are NULL, or there are no rows, the result is NULL. If
you require a well-formed XML document, you must ensure that your query is written so that the generated XML has a single
root element.
Data in BINARY, LONG BINARY, IMAGE, and VARBINARY columns is automatically returned in base64-encoded format when you execute
a query that contains XMLAGG.
For an example of a query that uses the XMLAGG function with an ORDER BY clause, see Use of the XMLAGG function.
SQL/2008
XMLAGG is part of optional SQL/2008 language feature X034. The optional ORDER BY clause for the XMLAGG function comprises
optional SQL/2008 language feature X035.
The following statement generates an XML document that shows the orders placed by each customer.
SELECT XMLELEMENT( NAME "order",
XMLATTRIBUTES( ID AS order_id ),
( SELECT XMLAGG(
XMLELEMENT(
NAME "Products",
XMLATTRIBUTES( ProductID, Quantity AS "quantity_shipped" ) ) )
FROM SalesOrderItems soi
WHERE soi.ID = so.ID
)
) AS products_ordered
FROM SalesOrders so
ORDER BY so.ID;