佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > oracle 樹查詢 語句

oracle 樹查詢 語句

熱門標(biāo)簽:鐵路電話系統(tǒng) AI電銷 服務(wù)外包 呼叫中心市場需求 Linux服務(wù)器 網(wǎng)站排名優(yōu)化 百度競價排名 地方門戶網(wǎng)站
格式:
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
例子:
原始數(shù)據(jù):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
需要實現(xiàn)得到結(jié)果是:
001 a01;a02;a03
002 b01
003 c01;c02
004 d01
005 e01;e02;e03;e04;e05
思路:
1、ORACLE8.1之后有個connect by 子句,取出整棵樹數(shù)據(jù)。
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 ?。危粒停拧。校粒遥牛危?
001 老王
101 老李
002 大王1 001
102 大李1 101
003 大王2 001
103 大李2 101
003 小王1 002
103 小李1 102
//按照家族樹取數(shù)據(jù)
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
結(jié)果:
1 /老王
2 /老王/大王1
3 /老王/大王1/小王1
2 /老王/大王2
1 /老李
2 /老李/大李1
3 /老李/大李1/小李1
2 /老李/大李2
按照上面思路,我們只要將原始數(shù)據(jù)做成如下結(jié)構(gòu):
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分組,取最大的一個值即為所需的結(jié)果。
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降序排序,產(chǎn)生偽列,目的是要形成樹結(jié)構(gòu)
NO ?。危粒停拧。遥?
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)
生成家族譜,即子節(jié)點與父節(jié)點有對應(yīng)關(guān)系,對應(yīng)關(guān)系通過rn和 rn1。其中l(wèi)ead為上一條記錄的RN值
NO ?。危粒停拧。遥巍 。遥危薄?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  ?。遥牛樱眨蹋?
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
將上面結(jié)果按照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
得到所需結(jié)果。

標(biāo)簽:仙桃 衡水 湘潭 黃山 湖南 崇左 銅川 蘭州

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《oracle 樹查詢 語句》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    阳西县| 民勤县| 四子王旗| 合肥市| 那曲县| 军事| 晋宁县| 富顺县| 长丰县| 南汇区| 和田市| 桐柏县| 灵石县| 罗江县| 江安县| 乌拉特前旗| 博乐市| 黄浦区| 安岳县| 哈尔滨市| 县级市| 秦皇岛市| 抚州市| 来宾市| 施秉县| 南木林县| 札达县| 河源市| 新宁县| 罗田县| 准格尔旗| 深泽县| 凤冈县| 阜宁县| 普宁市| 霍林郭勒市| 偏关县| 岳阳县| 祁东县| 高雄市| 德安县|