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
投稿

猜你喜欢

  • asp用正则解析远程图片地址,用XMLHTTP将其保存

    2007-10-26 12:34:00
  • 了解WEB页面工具语言XML(三)支持工具

    2008-09-05 17:18:00
  • 如何使用SQL Server中的客户端配置工具

    2009-01-13 14:05:00
  • js加密页面代码生成器

    2007-10-12 13:40:00
  • 如何操作Access数据库?

    2009-11-11 19:23:00
  • CSS经验:因为编码差异 导致IE6不能正常解析CSS文件

    2010-06-06 13:55:00
  • phpMyAdmin安装配置方法

    2009-09-09 20:15:00
  • 使用Spry轻松将XML数据显示到HTML页

    2007-11-16 16:44:00
  • img标签中alt和title属性的正确使用

    2008-01-10 12:59:00
  • 深入研究ASP中的Session

    2007-09-13 12:37:00
  • 用ASP和SQL实现基于Web日历源码

    2010-04-24 15:52:00
  • Asp.net实现简单的文字水印

    2007-08-24 09:28:00
  • ASP连接Access数据库和SQL server数据库的方法

    2007-08-22 13:16:00
  • SQLServer 连接 EXCEL

    2009-07-09 19:00:00
  • 用"表情符号"做植入广告 是否可行呢?

    2009-02-23 13:07:00
  • 用SQL语句添加删除修改字段、一些表与字段的基本操作、数据库备份等

    2011-12-01 07:53:11
  • ASP中类的详细介绍(class Property Get、Property Let)

    2008-02-20 19:18:00
  • 提升设计品质的8个布局方案[译]

    2010-03-18 16:06:00
  • FSO组件之文件操作(下)

    2010-05-03 11:10:00
  • 访问javascript私有变量

    2010-01-19 13:56:00
  • asp之家 网络编程 m.aspxhome.com