问题陈述:
您想在oracle中将字符串连接作为逗号分隔的文本。
解:
Oracle几乎没有执行字符串聚合的方法。您在互联网上最常用的用法是使用定界符将多行转换为单行。
从Oracle版本11.2开始,添加了一个名为listagg的新内置函数,该函数执行字符串聚合。listagg函数使用两个参数,第一个是我们要聚合的字符串列或表达式,第二个是可选参数是在聚合结果中放置在字符串之间的定界符。组内的命令是强制性的,并且要求我们指定order by子句,该子句告诉Oracle应该按什么顺序聚合字符串。
为了演示字符串聚合的用法,我们将使用students表在FI_ACCOUNT作业角色ID下查找资源。
SELECT class_id, first_name || ' ' || last_name FROM students WHERE class_id = 'INS_STAFF' ;
执行后,我们在工作角色下拥有5个资源。我们将使用listagg函数将这5个资源从多行转换为单行,并以逗号作为定界符。
INS_STAFF BROWN MICHAEL INS_STAFF JONES WILLIAM INS_STAFF MILLER DAVID INS_STAFF DAVIS RICHARD INS_STAFF GARCIA CHARLES
SELECT class_id, LISTAGG(first_name || ' ' || last_name , ', ' ) WITHIN GROUP (ORDER BY first_name) FROM students WHERE class_id = 'INS_STAFF' GROUP BY class_id ;INS_STAFF BROWN MICHAEL, DAVIS RICHARD, GARCIA CHARLES, JONES WILLIAM, MILLER DAVID
我们可以将其进一步应用于整个表的所有作业ID。
SELECT class_id, LISTAGG(first_name || ' ' || last_name , ', ' ) WITHIN GROUP (ORDER BY first_name) 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
listagg函数易于使用,并且是内置的高性能函数。有几个缺点:
它不能返回大于4,000或32,767字节的字符串。
在Oracle 19c版之前,它不能进行不同的聚合。
11.2之前的版本中不存在它。
数据在4000中不适合的最常见情况之一是数据不是唯一的。如果您的数据库版本为19c或更高版本,则可以进行不同的字符串聚合,从而使较少的匹配项可能适合varchar2。
SELECT class_id, LISTAGG( DISTINCT first_name || ' ' || last_name , ', ' ) WITHIN GROUP (ORDER BY first_name) 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
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;