OceanBase WMSYS.WM_CONCAT/WM_CONCAT
WM_CONCAT
函数用于列转行,将度量列的值进行合并。作为单个集合的聚合函数,WM_CONCAT
对所有行进行操作并返回单个输出行。作为组集聚合,WM_CONCAT
将对 GROUP BY
子句定义的每个组进行操作并返回输出行。作为分析函数,WM_CONCAT
基于 query_partition_clause
中的一个或多个表达式将查询结果集分为几组。
语法
WM_CONCAT ([DISTINCT] measure_expr) [OVER query_partition_clause]
作为分析函数使用时,您需要使用窗口函数的完整语法,它对一组行的集合进行计算并返回多个值。作为聚合函数使用时,该函数对一组行的集合进行聚合计算,结果只能返回一个值,此时不需要加 OVER
关键字。
参数
参数 |
说明 |
---|---|
OVER |
使用 |
measure_expr |
可以是任何表达式。度量列中的空值将被忽略。 |
DISTINCT |
如果指定 DISTINCT,会在返回结果中删除重复值。 |
返回类型
返回的数据类型为 CLOB
。
示例
分析函数示例
建表 employees,并向里面插入数据,执行以下语句:
CREATE TABLE employees (department_id INT,manager_id INT,last_name varchar(50),hiredate varchar(50),SALARY INT);
INSERT INTO employees VALUES(30, 100, 'Raphaely', '2017-07-01', 1700);
INSERT INTO employees VALUES(30, 100, 'De Haan', '2018-05-01',11000);
INSERT INTO employees VALUES(40, 100, 'Errazuriz', '2017-07-21', 1400);
INSERT INTO employees VALUES(50, 100, 'Hartstein', '2019-05-01',14000);
INSERT INTO employees VALUES(50, 100, 'Raphaely', '2017-07-22', 1700);
INSERT INTO employees VALUES(70, 100, 'Weiss', '2019-07-11',13500);
INSERT INTO employees VALUES(90, 100, 'Russell', '2019-10-05', 13000);
INSERT INTO employees VALUES(90,100, 'Partners', '2018-12-01',14000);
查询 2019 年 10 月 10 日之前雇用的雇员,以及该雇员的部门,雇用日期以及该部门中的其他雇员。执行以下语句:
SELECT department_id "Dept", hiredate "Date", last_name "Name",wmsys.wm_concat(last_name)
OVER (PARTITION BY department_id) as "Emp_list"
FROM employees WHERE hiredate < '2019-10-10' ORDER BY "Dept", "Date", "Name";
查询结果如下:
+------+------------+-----------+--------------------+
| Dept | Date | Name | Emp_list |
+------+------------+-----------+--------------------+
| 30 | 2017-07-01 | Raphaely | Raphaely,De Haan |
| 30 | 2018-05-01 | De Haan | Raphaely,De Haan |
| 40 | 2017-07-21 | Errazuriz | Errazuriz |
| 50 | 2017-07-22 | Raphaely | Hartstein,Raphaely |
| 50 | 2019-05-01 | Hartstein | Hartstein,Raphaely |
| 70 | 2019-07-11 | Weiss | Weiss |
| 90 | 2018-12-01 | Partners | Russell,Partners |
| 90 | 2019-10-05 | Russell | Russell,Partners |
+------+------------+-----------+--------------------+
8 rows in set (0.01 sec)
聚合函数示例
建表 employees,并向里面插入数据,执行以下语句:
CREATE TABLE employees (department_id INT,manager_id INT,last_name varchar(50),hiredate varchar(50),SALARY INT);
INSERT INTO employees VALUES(30, 100, 'Raphaely', '2017-07-01', 1700);
INSERT INTO employees VALUES(30, 100, 'De Haan', '2018-05-01',11000);
INSERT INTO employees VALUES(30, 100, 'Errazuriz', '2017-07-01', 1400);
INSERT INTO employees VALUES(30, 100, 'Hartstein', '2019-05-01',14000);
INSERT INTO employees VALUES(30, 100, 'Raphaely', '2017-07-01', 1700);
INSERT INTO employees VALUES(30, 100, 'Weiss', '2019-07-01',13500);
INSERT INTO employees VALUES(30, 100, 'Russell', '2019-07-01', 13000);
INSERT INTO employees VALUES(30,100, 'Partners', '2018-12-01',14000);
查询第 30 部门的所有员工,并查找最早的雇用日期。执行以下语句:
SELECT wmsys.wm_concat(last_name) as "Emp_list", MIN(hiredate) as "Earliest"
FROM employees WHERE department_id = 30;
查询结果如下:
+----------------------------------------------------------------------+------------+
| Emp_list | Earliest |
+----------------------------------------------------------------------+------------+
| Raphaely,De Haan,Errazuriz,Hartstein,Raphaely,Weiss,Russell,Partners | 2017-07-01 |
+----------------------------------------------------------------------+------------+
1 row in set (0.01 sec)