婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

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

oracle 樹查詢 語句

熱門標簽:地圖標注原件 淮安自動外呼系統開發 語音電話機器人營銷方案 百變地圖標注 修改高德地圖標注 南通防封外呼系統運營商 宜賓外呼系統廠家 廣州市400電話辦理 語音電話機器人缺點
格式:
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 樹查詢 語句》,本文關鍵詞  oracle,樹,查詢,語句,oracle,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《oracle 樹查詢 語句》相關的同類信息!
  • 本頁收集關于oracle 樹查詢 語句的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 文山县| 积石山| 池州市| 墨玉县| 定州市| 壶关县| 南岸区| 大埔区| 清流县| 怀来县| 贵溪市| 正镶白旗| 吴忠市| 朝阳市| 建宁县| 平凉市| 广东省| 老河口市| 潮安县| 绍兴市| 济南市| 连平县| 灌南县| 祁阳县| 天门市| 特克斯县| 报价| 阆中市| 朔州市| 勐海县| 夏津县| 梁河县| 虹口区| 喀什市| 综艺| 家居| 克山县| 南郑县| 泸溪县| 涡阳县| 伊宁县|