LISTAGG 函数用于列转行,LISTAGGORDER BY 子句中指定的每个组内的数据进行排序,然后合并度量列的值。作为单个集合的聚合函数,LISTAGG 对所有行进行操作并返回单个输出行。作为组集聚合,LISTAGG 将对 GROUP BY 子句定义的每个组进行操作并返回输出行。作为分析函数,LISTAGG 基于 query_partition_clause 中的一个或多个表达式将查询结果集分为几组。

语法

  1. LISTAGGmeasure_expr [,'delimiter'])WITHIN GROUP (order_by_clause)
  2. [OVER query_partition_clause]

作为分析函数使用时,您需要使用窗口函数的完整语法,它对一组行的集合进行计算并返回多个值。作为聚合函数使用时,该函数对一组行的集合进行聚合计算,结果只能返回一个值,此时不需要加 OVER 关键字。

参数

参数

说明

OVER

使用 OVER 子句定义窗口进行计算。

measure_expr

可以是任何表达式。度量列中的空值将被忽略。

delimiter

指定用于分隔度量值的字符串。此子句是可选的,默认为 NULL。

返回类型

如果度量列是 RAW 的,则返回数据类型为 RAW,否则返回值为 VARCHAR2 型。

示例

分析函数示例

建表 employees,并向里面插入数据,执行以下语句:

  1. CREATE TABLE employees (department_id INT,manager_id INT,last_name varchar(50),hiredate varchar(50),SALARY INT);
  2. INSERT INTO employees VALUES(30, 100, 'Raphaely', '2017-07-01', 1700);
  3. INSERT INTO employees VALUES(30, 100, 'De Haan', '2018-05-01',11000);
  4. INSERT INTO employees VALUES(40, 100, 'Errazuriz', '2017-07-21', 1400);
  5. INSERT INTO employees VALUES(50, 100, 'Hartstein', '2019-05-01',14000);
  6. INSERT INTO employees VALUES(50, 100, 'Raphaely', '2017-07-22', 1700);
  7. INSERT INTO employees VALUES(70, 100, 'Weiss', '2019-07-11',13500);
  8. INSERT INTO employees VALUES(90, 100, 'Russell', '2019-10-05', 13000);
  9. INSERT INTO employees VALUES(90,100, 'Partners', '2018-12-01',14000);

查询 2019 年 10 月 10 日之前雇用的雇员,以及该雇员的部门,雇用日期以及该部门中的其他雇员。执行以下语句:

  1. SELECT department_id "Dept", hiredate "Date", last_name "Name",LISTAGG(last_name, '; ') WITHIN GROUP
  2. (ORDER BY hiredate, last_name) OVER (PARTITION BY department_id) as "Emp_list"
  3. FROM employees WHERE hiredate < '2019-10-10' ORDER BY "Dept", "Date", "Name";

查询结果如下:

  1. +------+------------+-----------+---------------------+
  2. | Dept | Date | Name | Emp_list |
  3. +------+------------+-----------+---------------------+
  4. | 30 | 2017-07-01 | Raphaely | Raphaely; De Haan |
  5. | 30 | 2018-05-01 | De Haan | Raphaely; De Haan |
  6. | 40 | 2017-07-21 | Errazuriz | Errazuriz |
  7. | 50 | 2017-07-22 | Raphaely | Raphaely; Hartstein |
  8. | 50 | 2019-05-01 | Hartstein | Raphaely; Hartstein |
  9. | 70 | 2019-07-11 | Weiss | Weiss |
  10. | 90 | 2018-12-01 | Partners | Partners; Russell |
  11. | 90 | 2019-10-05 | Russell | Partners; Russell |
  12. +------+------------+-----------+---------------------+

聚合函数示例

建表 employees,并向里面插入数据,执行以下语句:

  1. CREATE TABLE employees (department_id INT,manager_id INT,last_name varchar(50),hiredate varchar(50),SALARY INT);
  2. INSERT INTO employees VALUES(30, 100, 'Raphaely', '2017-07-01', 1700);
  3. INSERT INTO employees VALUES(30, 100, 'De Haan', '2018-05-01',11000);
  4. INSERT INTO employees VALUES(30, 100, 'Errazuriz', '2017-07-01', 1400);
  5. INSERT INTO employees VALUES(30, 100, 'Hartstein', '2019-05-01',14000);
  6. INSERT INTO employees VALUES(30, 100, 'Raphaely', '2017-07-01', 1700);
  7. INSERT INTO employees VALUES(30, 100, 'Weiss', '2019-07-01',13500);
  8. INSERT INTO employees VALUES(30, 100, 'Russell', '2019-07-01', 13000);
  9. INSERT INTO employees VALUES(30,100, 'Partners', '2018-12-01',14000);

查询第 30 部门的所有员工,并按雇用日期和姓氏排序。执行以下语句:

  1. SELECT LISTAGG(last_name, '; ') WITHIN GROUP (ORDER BY hiredate, last_name) as "Emp_list",
  2. MIN(hiredate) as "Earliest" FROM employees WHERE department_id = 30;

查询结果如下:

  1. +-----------------------------------------------------------------------------+------------+
  2. | Emp_list | Earliest |
  3. +-----------------------------------------------------------------------------+------------+
  4. | Errazuriz; Raphaely; Raphaely; De Haan; Partners; Hartstein; Russell; Weiss | 2017-07-01 |
  5. +-----------------------------------------------------------------------------+------------+