17.10 - Example: Using XMLAGG to Aggregate Multiple Rows to Construct an XML Value - Advanced SQL Engine - Teradata Database

Teradata Vantageā„¢ - XML Data Type

Product
Advanced SQL Engine
Teradata Database
Release Number
17.10
Release Date
July 2021
Content Type
Programming Reference
Publication ID
B035-1140-171K
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;