16.20 - Example: Using XMLAGG to Aggregate Multiple Rows to Construct an XML Value - Teradata Database - Teradata Vantage NewSQL Engine

Teradata Vantage™ XML Data Type

Product
Teradata Database
Teradata Vantage NewSQL Engine
Release Number
16.20
Release Date
March 2019
Content Type
Programming Reference
Publication ID
B035-1140-162K
Language
English (United States)
The following query constructs a Department element that has an attribute "name" and child elements "emp" (a list of employees) ordered on the last names of the employees. One department element is created for each WORKDEPT.
SELECT XMLSERIALIZE(DOCUMENT XMLDOCUMENT
   (XMLELEMENT(NAME "Department",
      XMLATTRIBUTES(E.WORKDEPT AS "name"),
      XMLAGG(XMLELEMENT ( NAME "emp", E.LASTNAME)
      ORDER BY E.LASTNAME)
   )) AS CLOB(200)) AS "dept_list" 
   FROM EMPLOYEE E
   WHERE E.WORKDEPT IN ('C01', 'E21')
GROUP BY WORKDEPT;