如何使用Oracle聚合函数XMLAGG?
您想使用Oracle聚合函数XMLAGG进行字符串聚合。
解:
示例
SELECT class_id, rtrim(xmlagg(xmlelement(x,first_name || ' ' || last_name , ', ') ORDER BY first_name).extract('//text()').getstringval(), ',') AS "names" FROM students GROUP BY class_id;
输出结果
CL_MATH ANDERSON DANIEL, MARTINEZ CHRISTOPHER, TAYLOR PAUL, THOMAS MARK, WILSON THOMAS, CL_PHY RODRIGUEZ JOSEPH, INS_CHAIRMAN SMITH JAMES, INS_STAFF BROWN MICHAEL, DAVIS RICHARD, GARCIA CHARLES, JONES WILLIAM, MILLER DAVID, INS_VP JOHNSON JOHN, WILLIAMS ROBERT,
在上面的语句中,xmlelement将创建一个名为x的XML元素(随便命名),其中包含first_name,last_name和逗号的串联。
XMLAGG创建一个XML片段,是x的XML元素的集合。“.extract('//text()')”摆脱了xml的局面,仅保留字符串。最后,我们将使用rtrim删除聚合文本中的尾部逗号。
如果要使用CLOB而不是字符串,请参见以下代码。函数xmlparse将xml作为文本并将其转换为XMLType数据类型。
示例
SELECT class_id, xmlagg(xmlparse(content first_name || ' ' || last_name wellformed) ORDER BY first_name).getclobval() FROM students GROUP BY class_id;
输出结果
CL_MATH ANDERSON DANIELMARTINEZ CHRISTOPHERTAYLOR PAULTHOMAS MARKWILSON THOMAS CL_PHY RODRIGUEZ JOSEPH INS_CHAIRMAN SMITH JAMES INS_STAFF BROWN MICHAELDAVIS RICHARDGARCIA CHARLESJONES WILLIAMMILLER DAVID INS_VP JOHNSON JOHNWILLIAMS ROBERT
结果是一个CLOB,它显示在输出中。如果数据库版本为18c或更高版本,则可以使用json_arrayagg替代xmlagg。
示例
SELECT class_id, replace(JSON_ARRAYAGG(first_name || ' ' || last_name ORDER BY class_id RETURNING CLOB),'"','') AS retunring_val FROM students GROUP BY class_id;
输出结果
CL_MATH [WILSON THOMAS,THOMAS MARK,TAYLOR PAUL,ANDERSON DANIEL,MARTINEZ CHRISTOPHER] CL_PHY [RODRIGUEZ JOSEPH] INS_CHAIRMAN [SMITH JAMES] INS_STAFF [BROWN MICHAEL,GARCIA CHARLES,DAVIS RICHARD,MILLER DAVID,JONES WILLIAM] INS_VP [JOHNSON JOHN,WILLIAMS ROBERT]
数据准备:用于该问题的数据如下所示。
示例
DROP TABLE students; COMMIT; CREATE TABLE students ( student_id NUMBER(6) , first_name VARCHAR2(20) , last_name VARCHAR2(25) , email VARCHAR2(40) , phone_number VARCHAR2(20) , join_date DATE , class_id VARCHAR2(20) , fees NUMBER(8,2) , professor_id NUMBER(6) , department_id NUMBER(4) ) ;
示例
CREATE UNIQUE INDEX stu_id_pk ON students (student_id) ; INSERT INTO students VALUES (100,'SMITH','JAMES','SMITH.JAMES@notreal.com','111.111.1245',TO_DATE('17-06-2003','DD-MM-YYYY'),'INS_CHAIRMAN',24000,NULL,NULL); INSERT INTO students VALUES (101,'JOHNSON','JOHN','JOHNSON.JOHN@notreal.com','111.111.1246',TO_DATE('21-09-2005','DD-MM-YYYY'),'INS_VP',17000,100,90); INSERT INTO students VALUES (102,'WILLIAMS','ROBERT','WILLIAMS.ROBERT@notreal.com','111.111.1247',TO_DATE('13-01-2001','DD-MM-YYYY'),'INS_VP',17000,100,90); INSERT INTO students VALUES (103,'BROWN','MICHAEL','BROWN.MICHAEL@notreal.com','111.111.1248',TO_DATE('03-01-2006','DD-MM-YYYY'),'INS_STAFF',9000,102,60); INSERT INTO students VALUES (104,'JONES','WILLIAM','JONES.WILLIAM@notreal.com','111.111.1249',TO_DATE('21-05-2007','DD-MM-YYYY'),'INS_STAFF',6000,103,60); INSERT INTO students VALUES (105,'MILLER','DAVID','MILLER.DAVID@notreal.com','111.111.1250',TO_DATE('25-06-2005','DD-MM-YYYY'),'INS_STAFF',4800,103,60); INSERT INTO students VALUES (106,'DAVIS','RICHARD','DAVIS.RICHARD@notreal.com','111.111.1251',TO_DATE('05-02-2006','DD-MM-YYYY'),'INS_STAFF',4800,103,60); INSERT INTO students VALUES (107,'GARCIA','CHARLES','GARCIA.CHARLES@notreal.com','111.111.1252',TO_DATE('07-02-2007','DD-MM-YYYY'),'INS_STAFF',4200,103,60); INSERT INTO students VALUES (108,'RODRIGUEZ','JOSEPH','RODRIGUEZ.JOSEPH@notreal.com','111.111.1253',TO_DATE('17-08-2002','DD-MM-YYYY'),'CL_PHY',12008,101,100); INSERT INTO students VALUES (109,'WILSON','THOMAS','WILSON.THOMAS@notreal.com','111.111.1254',TO_DATE('16-08-2002','DD-MM-YYYY'),'CL_MATH',9000,108,100); INSERT INTO students VALUES (110,'MARTINEZ','CHRISTOPHER','MARTINEZ.CHRISTOPHER@notreal.com','111.111.1255',TO_DATE('28-09-2005','DD-MM-YYYY'),'CL_MATH',8200,108,100); INSERT INTO students VALUES (111,'ANDERSON','DANIEL','ANDERSON.DANIEL@notreal.com','111.111.1256',TO_DATE('30-09-2005','DD-MM-YYYY'),'CL_MATH',7700,108,100); INSERT INTO students VALUES (112,'TAYLOR','PAUL','TAYLOR.PAUL@notreal.com','111.111.1257',TO_DATE('07-03-2006','DD-MM-YYYY'),'CL_MATH',7800,108,100); INSERT INTO students VALUES (113,'THOMAS','MARK','THOMAS.MARK@notreal.com','111.111.1258',TO_DATE('07-12-2007','DD-MM-YYYY'),'CL_MATH',6900,108,100); COMMIT;