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

XML Data Type

Deployment
VantageCloud
VantageCore
Edition
Enterprise
IntelliFlex
VMware
Product
Analytics Database
Teradata Vantage
Release Number
17.20
Published
June 2022
Language
English (United States)
Last Update
2023-10-30
dita:mapPath
tkc1628112506748.ditamap
dita:ditavalPath
qkf1628213546010.ditaval
dita:id
dgs1472251600184
lifecycle
latest
Product Category
Teradata Vantageā„¢
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;