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

Teradata Vantage™ XML Data Type

prodname
Teradata Database
Teradata Vantage NewSQL Engine
vrm_release
16.20
category
Programming Reference
featnum
B035-1140-162K
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;