oracle 树查询 语句

来源:asp之家 时间:2009-07-17 18:20:00 

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

标签:oracle,树,查询
0
投稿

猜你喜欢

  • python调用matlab的m自定义函数方法

    2023-11-11 21:40:52
  • 基于循环神经网络(RNN)实现影评情感分类

    2021-11-27 16:42:10
  • golang 跳出for循环操作

    2024-04-30 10:04:00
  • Vscode 基础使用教程大全

    2023-03-06 01:58:18
  • Python根据成绩分析系统浅析

    2023-08-02 20:25:07
  • python基础知识之try...except...的详细用法实例

    2022-12-26 10:21:27
  • 原生JavaScript实现网页版计算器

    2024-04-23 09:27:08
  • asp和php下textarea提交大量数据发生丢失的解决方法

    2023-10-26 09:01:43
  • 关于基于字体大小(em)的设计

    2008-06-17 15:01:00
  • php巧获服务器端信息

    2023-10-04 02:18:39
  • Python模拟用户登录验证

    2021-07-12 19:58:28
  • 在django中,关于session的通用设置方法

    2023-09-26 09:53:52
  • python借助ChatGPT读取.env实现文件配置隔离保障私有数据安全

    2022-12-26 17:54:13
  • Python实现求解括号匹配问题的方法

    2023-08-02 08:05:47
  • python解决js文件utf-8编码乱码问题(推荐)

    2023-03-19 04:55:27
  • Python之csv文件从MySQL数据库导入导出的方法

    2023-08-09 04:45:10
  • Vue.js 时间转换代码及时间戳转时间字符串

    2024-04-30 10:21:53
  • oracle 多个字符替换实现

    2009-10-23 17:50:00
  • python数据结构链表之单向链表(实例讲解)

    2021-01-17 12:51:19
  • 让网站容易被发现

    2009-04-20 20:35:00
  • asp之家 网络编程 m.aspxhome.com