oracle

推荐列表 站点导航

当前位置:首页 > 数据库 > oracle >

oracle 树查询 语句

来源:网络整理  作者:网络  发布时间:2020-12-12 00:13
oracle 树查询,需要的朋友可以参考下,代码有点乱不好意思啊...
002 ;b01
001 ;a01;a02;a03
select lpad(' ',4*(level-1))||name name,job,id,super from emp
start with rn1 is null connect by rn1 = prior rn
1 /老王
005 e02
005 e05
NO NAME
005 e01/e02/e03
003 ;c01;c02
005 e01/e02
connect by prior id=super
002 b01
insert into a_example1
2 /老王/大王2
005 ;e01;e02;e03
select no,max(sys_connect_by_path(name,';')) result from
005 e01/e02/e03/e04
003 c01/c02
001 老王
values('003','大王2','001')
005 e01
001 a02 2
//按照家族树取数据
from ( select no,name,row_number() over(order by no,name desc) rn from a_example2)
例子:
start with super is null
正式生成树
003 c01;c02
003 c02
005 e02 11
)
格式:
003 大王2 001
3 /老李/大李1/小李1
001 a01
from (select no,name,row_number() over(order by no,name desc) rn from a_example2)
004 d01
(select no,name,rn,lead(rn) over(partition by no order by rn) rn1
003 小王1 002
connect by parent = prior no
values('103','小李1','102')
values('102','大李1','101')
103 小李1 102
create table a_example1
values('003','小王1','002')
NO NAME
001 a01;a02;a03
insert into a_example1
005 e01
005 ;e01;e02;e03;e04
values('103','大李2','101')
insert into a_example1
)
005 ;e01
004 ;d01
SQL语句:
NO  NAME RN
2 /老李/大李2
1、 select no,name,row_number() over(order by no,name desc) rn from a_example2
按照上面思路,我们只要将原始数据做成如下结构:
values('001','老王',null)
001 a01 3
insert into a_example1
NO NAME
004 d01
NO  NAME RN  RN1  001 a03 1 2 --
将上面结果按照NO分组,取result最大值即可,所以将上述语句改为
002 b01
003 c02 5
002 b01 4
no char(3) not null,
语句分析:
003 c01
insert into a_example1
from ( select no,name,row_number() over(order by no,name desc) rn from a_example2))
from a_example1
103 大李2 101
按照NO升序排序,同时按照NAME降序排序,产生伪列,目的是要形成树结构
parent char(3)
005 e04 9
FROM table_name
002 大王1 001
CONNECT BY PRIOR 父主键=子外键
NO   RESULT
004 d01 7
得到所需结果。
001 a01/a02
004 d01
003 ;c01
001 a01/a02/a03/a04/a05
005 e01/e02/e03/e04/e05
001 a05
004 d01
005 ;e01;e02;e03;e04;e05
)
start with /*name = '老王' and*/ parent is null
102 大李1 101
3 /老王/大王1/小王1
---------- ------------------------------
values('101','老李',null)
(select no,name,rn,lead(rn) over(partition by no order by rn) rn1
name varchar2(10) not null,
最后按NO分组,取最大的一个值即为所需的结果。
003 c01/c02
需要实现得到结果是:
001 a01/a02/a03/a04/a05
(
from (select no,name,row_number() over(order by no,name desc) rn from a_example2)
select no,max(sys_connect_by_path(name,';')) result from
001 a01/a02/a03/a04
002 b01 4 003 c02 5 6 003 c01 6 004 d01 7 005 e05 8 9 005 e04 9 10 005 e03 10 11 005 e02 11 12 005 e01 12
2 /老王/大王1
insert into a_example1
group by no
insert into a_example1
start with rn1 is null connect by rn1 = prior rn
values('002','大王1','001')
003 c01 6
2、select no,name,rn,lead(rn) over(partition by no order by rn) rn1
3、select no,sys_connect_by_path(name,';') result from
1 /老李
001 a02
005 e01/e02/e03/e04/e05
生成家族谱,即子节点与父节点有对应关系,对应关系通过rn和 rn1。其中lead为上一条记录的RN值
005 e01;e02;e03;e04;e05
001 a01/a02/a03
001 ;a01;a02
005 e04
001 a03 1
002 b01
select level,sys_connect_by_path(name,'/') path
START WITH column=value
001 a03
001 ;a01
结果:
1、ORACLE8.1之后有个connect by 子句,取出整棵树数据。
005 ;e01;e02
2 /老李/大李1
003 c01
insert into a_example1
101 老李
start with rn1 is null connect by rn1 = prior rn
002 b01
思路:
001 a01
select * from a_example1
说明:针对NO=001来说,其下一条记录的RN=2 001 a02 2 3 --说明:针对NO=001来说,其下一条记录的RN=3 001 a01 3  --说明:针对NO=001来说,其下一条记录的RN IS NULL
005 e03 10
NO  NAME PARENT
005 e03
005 e05 8
group by no
(select no,name,rn,lead(rn) over(partition by no order by rn) rn1
001 a04
SELECT column
原始数据:select no,q from a_example2
005 e01 12

相关热词: oracle

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!

本文地址: https://v30.fanwenzhu.com/sql/oracle/2728.shtml

Copyright © www.juheyunku.com      关于 | 合作 | 声明 | 联系 | 更新 | 地图 | Tags

oracle 树查询 语句

2020-12-12 编辑:网络

002 ;b01
001 ;a01;a02;a03
select lpad(' ',4*(level-1))||name name,job,id,super from emp
start with rn1 is null connect by rn1 = prior rn
1 /老王
005 e02
005 e05
NO NAME
005 e01/e02/e03
003 ;c01;c02
005 e01/e02
connect by prior id=super
002 b01
insert into a_example1
2 /老王/大王2
005 ;e01;e02;e03
select no,max(sys_connect_by_path(name,';')) result from
005 e01/e02/e03/e04
003 c01/c02
001 老王
values('003','大王2','001')
005 e01
001 a02 2
//按照家族树取数据
from ( select no,name,row_number() over(order by no,name desc) rn from a_example2)
例子:
start with super is null
正式生成树
003 c01;c02
003 c02
005 e02 11
)
格式:
003 大王2 001
3 /老李/大李1/小李1
001 a01
from (select no,name,row_number() over(order by no,name desc) rn from a_example2)
004 d01
(select no,name,rn,lead(rn) over(partition by no order by rn) rn1
003 小王1 002
connect by parent = prior no
values('103','小李1','102')
values('102','大李1','101')
103 小李1 102
create table a_example1
values('003','小王1','002')
NO NAME
001 a01;a02;a03
insert into a_example1
005 e01
005 ;e01;e02;e03;e04
values('103','大李2','101')
insert into a_example1
)
005 ;e01
004 ;d01
SQL语句:
NO  NAME RN
2 /老李/大李2
1、 select no,name,row_number() over(order by no,name desc) rn from a_example2
按照上面思路,我们只要将原始数据做成如下结构:
values('001','老王',null)
001 a01 3
insert into a_example1
NO NAME
004 d01
NO  NAME RN  RN1  001 a03 1 2 --
将上面结果按照NO分组,取result最大值即可,所以将上述语句改为
002 b01
003 c02 5
002 b01 4
no char(3) not null,
语句分析:
003 c01
insert into a_example1
from ( select no,name,row_number() over(order by no,name desc) rn from a_example2))
from a_example1
103 大李2 101
按照NO升序排序,同时按照NAME降序排序,产生伪列,目的是要形成树结构
parent char(3)
005 e04 9
FROM table_name
002 大王1 001
CONNECT BY PRIOR 父主键=子外键
NO   RESULT
004 d01 7
得到所需结果。
001 a01/a02
004 d01
003 ;c01
001 a01/a02/a03/a04/a05
005 e01/e02/e03/e04/e05
001 a05
004 d01
005 ;e01;e02;e03;e04;e05
)
start with /*name = '老王' and*/ parent is null
102 大李1 101
3 /老王/大王1/小王1
---------- ------------------------------
values('101','老李',null)
(select no,name,rn,lead(rn) over(partition by no order by rn) rn1
name varchar2(10) not null,
最后按NO分组,取最大的一个值即为所需的结果。
003 c01/c02
需要实现得到结果是:
001 a01/a02/a03/a04/a05
(
from (select no,name,row_number() over(order by no,name desc) rn from a_example2)
select no,max(sys_connect_by_path(name,';')) result from
001 a01/a02/a03/a04
002 b01 4 003 c02 5 6 003 c01 6 004 d01 7 005 e05 8 9 005 e04 9 10 005 e03 10 11 005 e02 11 12 005 e01 12
2 /老王/大王1
insert into a_example1
group by no
insert into a_example1
start with rn1 is null connect by rn1 = prior rn
values('002','大王1','001')
003 c01 6
2、select no,name,rn,lead(rn) over(partition by no order by rn) rn1
3、select no,sys_connect_by_path(name,';') result from
1 /老李
001 a02
005 e01/e02/e03/e04/e05
生成家族谱,即子节点与父节点有对应关系,对应关系通过rn和 rn1。其中lead为上一条记录的RN值
005 e01;e02;e03;e04;e05
001 a01/a02/a03
001 ;a01;a02
005 e04
001 a03 1
002 b01
select level,sys_connect_by_path(name,'/') path
START WITH column=value
001 a03
001 ;a01
结果:
1、ORACLE8.1之后有个connect by 子句,取出整棵树数据。
005 ;e01;e02
2 /老李/大李1
003 c01
insert into a_example1
101 老李
start with rn1 is null connect by rn1 = prior rn
002 b01
思路:
001 a01
select * from a_example1
说明:针对NO=001来说,其下一条记录的RN=2 001 a02 2 3 --说明:针对NO=001来说,其下一条记录的RN=3 001 a01 3  --说明:针对NO=001来说,其下一条记录的RN IS NULL
005 e03 10
NO  NAME PARENT
005 e03
005 e05 8
group by no
(select no,name,rn,lead(rn) over(partition by no order by rn) rn1
001 a04
SELECT column
原始数据:select no,q from a_example2
005 e01 12

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供学习参考!
本文地址为 https://v30.fanwenzhu.com/sql/oracle/2728.shtml

相关文章

风云图片

推荐阅读

返回oracle频道首页