My version is10.5,After testing, DB2 V9.7.0.10 can also be used (I don’t know about other versions, it seems to be 9.7 and above), if you don’t know your own version, you can use the following sql to check:
xmlelement,xmlelement() is a function that specifies XML elements. This function requires the following two parameters:
name: Specifies the name of the generated XML tag element.
[Data that needs to be spliced]: The data contained in the element can be one or more values, which can be spliced and separated by custom delimiters.
xmlagg()The function will concatenate the results of all XML elements into an XML document in a sequential manner, thereby returning an XML type value.
Implementation code:
SELECT DEPT_ID ,xmlagg(xmlelement(NAME userName, su.USER_NAME||','))AS all_user_names
FROM SYS_USER su
WHERE DEPT_ID ='D001'GROUPBY DEPT_ID;
The implementation code (plus sorting) is as follows:
SELECT DEPT_ID ,xmlagg(
xmlelement(NAME userName, su.USER_NAME||',')ORDERBY su.USER_NAME
)AS all_user_names
FROM SYS_USER su
WHERE DEPT_ID ='D001'GROUPBY DEPT_ID;
SELECT DEPT_ID ,replace(replace(xml2clob(xmlagg(xmlelement(NAME userName, su.USER_NAME||','))),'<USERNAME>',''),'</USERNAME>',' ')AS all_user_names
FROM SYS_USER su
WHERE DEPT_ID ='D001'GROUPBY DEPT_ID;