lightdb22.3预览-listagg 增强
Posted 紫无之紫
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了lightdb22.3预览-listagg 增强相关的知识,希望对你有一定的参考价值。
lightdb22.3 - listagg 增强
LightDB 在22.3版本对listagg 进行了增强,现在支持同时使用within group(order by xxx ) 和over(partition by xxx)
oracle listagg
oracle 19c 中listagg 语法如下用法:
DISTINCT 从19c 开始支持
LightDB listagg
之前版本限制:
-
不支持 WITHIN GROUP order_by_clause 与 OVER query_partition_clause一起用。
-
不支持DISTINCT 与 OVER query_partition_clause一起使用
22.3 版本取出来上述1的限制,目前只有如下限制:
- 不支持DISTINCT 与 WITHIN GROUP order_by_clause OVER query_partition_clause 一起使用,其实就是上述的2
案例
表结构及数据:
CREATE TABLE EMP
( EMPNO NUMBER(4, 0),
ENAME VARCHAR2(10),
JOB VARCHAR2(9),
MGR NUMBER(4, 0),
HIREDATE DATE,
SAL NUMBER(7, 2),
COMM NUMBER(7, 2),
DEPTNO NUMBER(2, 0),
DNAME VARCHAR2(100),
CONSTRAINT PK_EMP PRIMARY KEY (EMPNO)
);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO, DNAME) values (7369, 'SMITH', 'CLERK', 7902, to_date('1980-12-17','yyyy-mm-dd'), 800.00, null, 20, null);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO, DNAME) values (7499, 'ALLEN', 'SALESMAN', 7698, to_date('1981-02-20','yyyy-mm-dd'), 1600.00, 300.00, 30, null);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO, DNAME) values (7521, 'WARD', 'SALESMAN', 7698, to_date('1981-02-22','yyyy-mm-dd'), 1250.00, 500.00, 30, null);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO, DNAME) values (7566, 'JONES', 'MANAGER', 7839, to_date('1981-04-02','yyyy-mm-dd'), 2975.00, null, 20, null);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO, DNAME) values (7654, 'MARTIN', 'SALESMAN', 7698, to_date('1981-09-28','yyyy-mm-dd'), 1250.00, 1400.00, 30, null);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO, DNAME) values (7698, 'BLAKE', 'MANAGER', 7839, to_date('1981-05-01','yyyy-mm-dd'), 2850.00, null, 30, null);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO, DNAME) values (7782, 'CLARK', 'MANAGER', 7839, to_date('1981-06-09','yyyy-mm-dd'), 2450.00, null, 10, null);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO, DNAME) values (7788, 'SCOTT', 'ANALYST', 7566, to_date('1987-04-19','yyyy-mm-dd'), 3000.00, null, 20, null);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO, DNAME) values (7839, 'KING', 'PRESIDENT',null, to_date('1981-11-17','yyyy-mm-dd'), 5000.00, null, 10, null);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO, DNAME) values (7844, 'TURNER', 'SALESMAN', 7698, to_date('1981-09-08','yyyy-mm-dd'), 1500.00, 0.00, 30, null);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO, DNAME) values (7876, 'ADAMS', 'CLERK', 7788, to_date('1987-05-23','yyyy-mm-dd'), 1100.00, null, 20, null);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO, DNAME) values (7900, 'JAMES', 'CLERK', 7698, to_date('1981-12-03','yyyy-mm-dd'), 950.00, null, 30, null);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO, DNAME) values (7902, 'FORD', 'ANALYST', 7566, to_date('1981-12-03','yyyy-mm-dd'), 3000.00, null, 20, null);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO, DNAME) values (7934, 'MILLER', 'CLERK', 7782, to_date('1982-01-23','yyyy-mm-dd'), 1300.00, null, 10, null);
查询:
select listagg(ename, ',') within group(order by ename) over(partition by deptno) as enames,
deptno,
ename
from EMP;
enames | deptno | ename
--------------------------------------+--------+--------
CLARK,KING,MILLER | 10 | CLARK
CLARK,KING,MILLER | 10 | KING
CLARK,KING,MILLER | 10 | MILLER
ADAMS,FORD,JONES,SCOTT,SMITH | 20 | ADAMS
ADAMS,FORD,JONES,SCOTT,SMITH | 20 | FORD
ADAMS,FORD,JONES,SCOTT,SMITH | 20 | JONES
ADAMS,FORD,JONES,SCOTT,SMITH | 20 | SCOTT
ADAMS,FORD,JONES,SCOTT,SMITH | 20 | SMITH
ALLEN,BLAKE,JAMES,MARTIN,TURNER,WARD | 30 | ALLEN
ALLEN,BLAKE,JAMES,MARTIN,TURNER,WARD | 30 | BLAKE
ALLEN,BLAKE,JAMES,MARTIN,TURNER,WARD | 30 | JAMES
ALLEN,BLAKE,JAMES,MARTIN,TURNER,WARD | 30 | MARTIN
ALLEN,BLAKE,JAMES,MARTIN,TURNER,WARD | 30 | TURNER
ALLEN,BLAKE,JAMES,MARTIN,TURNER,WARD | 30 | WARD
(14 rows)
不支持distinct:
chuhx@test=# select listagg(distinct ename, ',') within group(order by ename) over(partition by deptno) as enames, deptno, ename from EMP;
ERROR: DISTINCT is not implemented for window functions
LINE 1: select listagg(distinct ename, ',') within group(order by en...
参考
以上是关于lightdb22.3预览-listagg 增强的主要内容,如果未能解决你的问题,请参考以下文章
lightdb22.3特性预览-增强对oracle内置函数的兼容