如何在Oracle中执行字符串聚合/连接?
问题陈述
您想在Oracle中将字符串连接为逗号分隔的文本。
解决方案
Oracle有几种方法可以执行字符串聚合。在互联网上流行的常见用法是将多行转换为带分隔符的单行。
从Oracle 11.2版本开始,添加了一个新的内置函数,称为listagg,它执行字符串聚合。listagg函数使用两个参数,第一个是我们想要聚合的字符串列或表达式,第二个是可选参数,是在聚合结果中的字符串之间添加的分隔符。`within group`是强制性的,它要求我们指定一个`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
SQL:字符串聚合
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
我们可以进一步将此应用于所有Job ID的整个表。
示例
SELECT class_id, LISTAGG(first_name || ' ' || last_name , ', ' ) WITHIN GROUP (ORDER BY first_name) FROM students GROUP BY class_id ;
输出:SQL中的几行
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之前,它不能进行distinct聚合。
- 在11.2之前的版本中不存在。
数据不适合4000字节的最常见情况之一是数据不是唯一的。如果您的数据库版本为19c或更高版本,您可以进行distinct字符串聚合,使较少的出现次数可能适合varchar2。
SQL:带有Distinct子句的字符串聚合
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','[email protected]','111.111.1245',TO_DATE('17-06-2003','DD-MM-YYYY'),'INS_CHAIRMAN',24000,NULL,NULL); INSERT INTO students VALUES (101,'JOHNSON','JOHN','[email protected]','111.111.1246',TO_DATE('21-09-2005','DD-MM-YYYY'),'INS_VP',17000,100,90); INSERT INTO students VALUES (102,'WILLIAMS','ROBERT','[email protected]','111.111.1247',TO_DATE('13-01-2001','DD-MM-YYYY'),'INS_VP',17000,100,90); INSERT INTO students VALUES (103,'BROWN','MICHAEL','[email protected]','111.111.1248',TO_DATE('03-01-2006','DD-MM-YYYY'),'INS_STAFF',9000,102,60); INSERT INTO students VALUES (104,'JONES','WILLIAM','[email protected]','111.111.1249',TO_DATE('21-05-2007','DD-MM-YYYY'),'INS_STAFF',6000,103,60); INSERT INTO students VALUES (105,'MILLER','DAVID','[email protected]','111.111.1250',TO_DATE('25-06-2005','DD-MM-YYYY'),'INS_STAFF',4800,103,60); INSERT INTO students VALUES (106,'DAVIS','RICHARD','[email protected]','111.111.1251',TO_DATE('05-02-2006','DD-MM-YYYY'),'INS_STAFF',4800,103,60); INSERT INTO students VALUES (107,'GARCIA','CHARLES','[email protected]','111.111.1252',TO_DATE('07-02-2007','DD-MM-YYYY'),'INS_STAFF',4200,103,60); INSERT INTO students VALUES (108,'RODRIGUEZ','JOSEPH','[email protected]','111.111.1253',TO_DATE('17-08-2002','DD-MM-YYYY'),'CL_PHY',12008,101,100); INSERT INTO students VALUES (109,'WILSON','THOMAS','[email protected]','111.111.1254',TO_DATE('16-08-2002','DD-MM-YYYY'),'CL_MATH',9000,108,100); INSERT INTO students VALUES (110,'MARTINEZ','CHRISTOPHER','[email protected]','111.111.1255',TO_DATE('28-09-2005','DD-MM-YYYY'),'CL_MATH',8200,108,100); INSERT INTO students VALUES (111,'ANDERSON','DANIEL','[email protected]','111.111.1256',TO_DATE('30-09-2005','DD-MM-YYYY'),'CL_MATH',7700,108,100); INSERT INTO students VALUES (112,'TAYLOR','PAUL','[email protected]','111.111.1257',TO_DATE('07-03-2006','DD-MM-YYYY'),'CL_MATH',7800,108,100); INSERT INTO students VALUES (113,'THOMAS','MARK','[email protected]','111.111.1258',TO_DATE('07-12-2007','DD-MM-YYYY'),'CL_MATH',6900,108,100); COMMIT;