demo场景,以oracle自带库中的表emp为例:
select ename,deptno from emp order by deptno;
ENAME | DEPTNO | @H_502_23@
CLARK | 10 | @H_502_23@
KING | 10 | @H_502_23@
MILLER | 10 | @H_502_23@
SMITH | 20 | @H_502_23@
ADAMS | 20 | @H_502_23@
FORD | 20 | @H_502_23@
SCOTT | 20 | @H_502_23@
JONES | 20 | @H_502_23@
ALLEN | 30 | @H_502_23@
BLAKE | 30 | @H_502_23@
MARTIN | 30 | @H_502_23@
JAMES | 30 | @H_502_23@
TURNER | 30 | @H_502_23@
WARD | 30 | @H_502_23@
现在想要将同一部门的人给合并成一行记录,如何做呢?如下:
ENAME | DEPTNO | @H_502_23@
CLARK,KING,MILLER | 10 | @H_502_23@
ADAMS,FORD,JONES,SCOTT,SMITH | 20 | @H_502_23@
ALLEN,BLAKE,JAMES,MARTIN,TURNER,WARD | 30 | @H_502_23@
通常我们都是自己写函数或在程序中处理,这里我们利用oracle自带的分析函数row_number()和sys_connect_by_path来进行sql语句层面的多行到单行的合并,并且效率会非常高。
基本思路:
1、对deptno进行row_number()按ename排位并打上排位号
select deptno,ename,row_number() over(partition by deptno order by deptno,ename) rank
from emp order by deptno,ename;
DEPTNO | ENAME | RANK | @H_502_23@
10 | CLARK | 1 | @H_502_23@
10 | KING | 2 | @H_502_23@
10 | MILLER | 3 | @H_502_23@
20 | ADAMS | 1 | @H_502_23@
20 | FORD | 2 | @H_502_23@
20 | JONES | 3 | @H_502_23@
20 | SCOTT | 4 | @H_502_23@
20 | SMITH | 5 | @H_502_23@
30 | ALLEN | 1 | @H_502_23@
30 | BLAKE | 2 | @H_502_23@
30 | JAMES | 3 | @H_502_23@
30 | MARTIN | 4 | @H_502_23@
30 | TURNER | 5 | @H_502_23@
30 | WARD | 6 | @H_502_23@
可看出,经过row_number()后,部门人已经按部门和人名进行了排序,并打上了一个位置字段rank
2、利用oracle的递归查询connect by进行表内递归,并通过sys_connect_by_path进行父子数据追溯串的构造,这里要针对ename字段进行构造,使之合并在一个字段内(数据很多,只截取部分)
select deptno,rank,level as curr_level,
ltrim(sys_connect_by_path(ename,','),') ename_path from (
select deptno,ename) connect by deptno = prior deptno and rank-1 = prior rank;
各部门递归后的数据量都是:(1+n)/2 * n 即:deptno=10 数据量:(1+3)/2 * 3 = 6;
deptno=20 数据量:(1+5)/2 * 5 = 15;deptno=30 数据量:(1+6)/2 * 6 = 21;
DEPTNO | ENAME | RANK | CURR_LEVEL | ENAME_PATH | @H_502_23@
10 | CLARK | 1 | 1 | CLARK | @H_502_23@
10 | KING | 2 | 2 | CLARK,KING | @H_502_23@
10 | MILLER | 3 | 3 | CLARK,MILLER | @H_502_23@
10 | KING | 2 | 1 | KING | @H_502_23@
10 | MILLER | 3 | 2 | KING,MILLER | @H_502_23@
10 | MILLER | 3 | 1 | MILLER | @H_502_23@
DEPTNO | ENAME | RANK | CURR_LEVEL | ENAME_PATH | @H_502_23@
20 | ADAMS | 1 | 1 | ADAMS | @H_502_23@
20 | FORD | 2 | 2 | ADAMS,FORD | @H_502_23@
20 | JONES | 3 | 3 | ADAMS,JONES | @H_502_23@
20 | SCOTT | 4 | 4 | ADAMS,SCOTT | @H_502_23@
20 | SMITH | 5 | 5 | ADAMS,SMITH | @H_502_23@
20 | FORD | 2 | 1 | FORD | @H_502_23@
20 | JONES | 3 | 2 | FORD,JONES | @H_502_23@
20 | SCOTT | 4 | 3 | FORD,SCOTT | @H_502_23@
20 | SMITH | 5 | 4 | FORD,SMITH | @H_502_23@
20 | JONES | 3 | 1 | JONES | @H_502_23@
20 | SCOTT | 4 | 2 | JONES,SCOTT | @H_502_23@
20 | SMITH | 5 | 3 | JONES,SMITH | @H_502_23@
20 | SCOTT | 4 | 1 | SCOTT | @H_502_23@
20 | SMITH | 5 | 2 | SCOTT,SMITH | @H_502_23@
20 | SMITH | 5 | 1 | SMITH | @H_502_23@
这里我们仅列出deptno=10、20的,至此我们应该能否发现一些线索了,即每个部门中,curr_level最高的那行,有我们所需要的数据。那后面该怎么办,取出那个数据? 对了,继续用row_number()进行排位标记,然后再按排位标记取出即可。
3、 对deptno继续进行row_number()按curr_level排位
select deptno,ename_path,curr_level desc) ename_path_rank from (select deptno,ename) connect by deptno = prior deptno and rank-1 = prior rank);
DEPTNO | ENAME_PATH | ENAME_PATH_RANK | @H_502_23@
10 | CLARK,MILLER | 1 | @H_502_23@
10 | CLARK,KING | 2 | @H_502_23@
10 | KING,MILLER | 3 | @H_502_23@
10 | CLARK | 4 | @H_502_23@
10 | KING | 5 | @H_502_23@
10 | MILLER | 6 | @H_502_23@
DEPTNO | ENAME_PATH | ENAME_PATH_RANK | @H_502_23@
20 | ADAMS,SMITH | 1 | @H_502_23@
20 | ADAMS,SCOTT | 2 | @H_502_23@
20 | FORD,SMITH | 3 | @H_502_23@
20 | ADAMS,JONES | 4 | @H_502_23@
20 | FORD,SCOTT | 5 | @H_502_23@
20 | JONES,SMITH | 6 | @H_502_23@
20 | ADAMS,FORD | 7 | @H_502_23@
20 | FORD,JONES | 8 | @H_502_23@
20 | SCOTT,SMITH | 9 | @H_502_23@
20 | JONES,SCOTT | 10 | @H_502_23@
20 | ADAMS | 11 | @H_502_23@
20 | JONES | 12 | @H_502_23@
20 | SMITH | 13 | @H_502_23@
20 | SCOTT | 14 | @H_502_23@
20 | FORD | 15 | @H_502_23@
这里还是仅列出deptno为10、20的,至此应该很明了了,在进行一次查询,取ename_path_rank为1的即可获得我们想要的结果。
4、获取想要排位的数据,即得部门下所有人多行到单行的合并
select deptno,ename_path from (select deptno,
row_number() over(partition by deptno order by deptno,curr_level desc) ename_path_rank
from (select deptno,ename) connect by deptno = prior deptno and rank-1 = prior rank))
where ename_path_rank=1;
from(selectdeptno,
ename_path,
row_number()over(partitionbydeptnoorderbydeptno,curr_leveldesc)ename_path_rank
from(
selectempno,
deptno,
ename,
rank,
levelascurr_level,
ltrim(sys_connect_by_path(ename,','),')ename_path
from(selectdeptno,
empno,ename)rank
fromemp
orderbydeptno,ename)
connectbydeptno=priordeptno
andrank-1=priorrank
)) whereename_path_rank=1;