[英]Hierarchical Query to Self-Join Table Max 3 Times
我有一個帶有以下各列的表的Oracle DB:
ID | PARENTID | DETAIL1
------------------------
1 | NULL | BLAH1
2 | 1 | BLAH2
3 | 2 | BLAH3
4 | 2 | BLAH4
5 | NULL | BLAH5
6 | 5 | BLAH6
7 | 6 | BLAH7
8 | 5 | BLAH8
9 | 5 | BLAH9
10 | 8 | BLAH10
我准備了一個自我加入
SELECT PARENT.ID AS "PID",
PARENT.DETAIL1 AS "PDETAIL1",
CHILD.ID AS "CID",
CHILD.DETAIL1 AS "CDETAIL1"
FROM table1 CHILD
LEFT OUTER JOIN table1 PARENT
ON PARENT.ID = CHILD.PARENTID
WHERE PARENTID IS NOT NULL;
輸出如下所示:
PID | PDETAIL1 | CID | CDETAIL1|
--------------------------------
1 | BLAH1 | 2 | BLAH2 |
2 | BLAH2 | 3 | BLAH3 |
2 | BLAH2 | 4 | BLAH4 |
5 | BLAH5 | 6 | BLAH6 |
6 | BLAH6 | 7 | BLAH7 |
5 | BLAH5 | 8 | BLAH8 |
5 | BLAH5 | 9 | BLAH9 |
8 | BLAH8 | 10 | BLAH10 |
非常簡單。 我想知道是否可以將這種自我聯接作為層次/遞歸查詢來完成。 最大嵌套深度為3。目標輸出應如下所示:
GPID | GPDETAIL1 | PID | PDETAIL1 | CID | CDETAIL1 |
---------------------------------------------------
1 | BLAH1 | 2 | BLAH2 | 3 | BLAH3 |
1 | BLAH1 | 2 | BLAH2 | 4 | BLAH4 |
5 | BLAH5 | 6 | BLAH6 | 7 | BLAH7 |
5 | BLAH5 | 8 | BLAH8 | 10 | BLAH10 |
5 | BLAH5 | 9 | BLAH9 | NULL | NULL |
Google並沒有幫助我,有大量與分層查詢有關的信息,但是沒有任何信息(包括自我聯接和分層查詢),而且大多數問題在表面上看起來都是相似的,但是沒有什么可以指導我滿足我的需求。 我是SQL新手,因此,除非答案是特定的,否則我可能會錯過它。
您只需要再join
一次餐桌就可以得到孩子們。 因此,基本上有一個祖父母別名,父別名和子別名,並相應地join
:
select
gp.id as gpid,
gp.detail as gpdetail1,
p.id as pid,
p.detail as pdetail1,
c.id as cid,
c.detail as cdetail1
from yourtable gp
left join yourtable p on gp.id = p.parentid
left join yourtable c on p.id = c.parentid
where gp.parentid is null
有大量與分層查詢有關的信息,但沒有任何信息包括自我聯接和分層查詢
您不需要兩者,層次查詢是自連接。
您可以從類似於以下的分層查詢開始:
select connect_by_root (id) as gpid, connect_by_root(detail1) as gpdetail1,
prior id as pid, prior detail1 as pdetail1,
id as cid, detail1 as cdetail1,
level as lvl, connect_by_isleaf as is_leaf
from table1
start with parentid is null
connect by prior id = parentid
請參閱文檔 ,了解connect_by_root和connect_by_isleaf的含義。 您對葉節點感興趣,但是:
select *
from (
select connect_by_root (id) as gpid, connect_by_root(detail1) as gpdetail1,
prior id as pid, prior detail1 as pdetail1,
id as cid, detail1 as cdetail1,
level as lvl, connect_by_isleaf as is_leaf
from table1
start with parentid is null
connect by prior id = parentid
)
where is_leaf = 1;
...沒有得到您想要的東西:
GPID GPDETA PID PDETAI CID CDETAI LVL IS_LEAF
---------- ------ ---------- ------ ---------- ------ ---------- ----------
1 BLAH1 2 BLAH2 3 BLAH3 3 1
1 BLAH1 2 BLAH2 4 BLAH4 3 1
5 BLAH5 6 BLAH6 7 BLAH7 3 1
5 BLAH5 8 BLAH8 10 BLAH10 3 1
5 BLAH5 5 BLAH5 9 BLAH9 2 1
從示例輸出中,您不希望在最后一行的父列中使用5 / BLAH5,因為它們是祖父母。 您希望將子級值提升為父級狀態。 不過,您可以稍微操縱父級和子級值:
select gpid, gpdetail1,
case lvl when 2 then cid else pid end as pid,
case lvl when 2 then cdetail1 else pdetail1 end as pdetail1,
case lvl when 2 then null else cid end as cid,
case lvl when 2 then null else cdetail1 end as cdetail1
from (
select connect_by_root (id) as gpid, connect_by_root(detail1) as gpdetail1,
prior id as pid, prior detail1 as pdetail1,
id as cid, detail1 as cdetail1,
level as lvl, connect_by_isleaf as is_leaf
from table1
start with parentid is null
connect by prior id = parentid
)
where is_leaf = 1;
GPID GPDETA PID PDETAI CID CDETAI
---------- ------ ---------- ------ ---------- ------
1 BLAH1 2 BLAH2 3 BLAH3
1 BLAH1 2 BLAH2 4 BLAH4
5 BLAH5 6 BLAH6 7 BLAH7
5 BLAH5 8 BLAH8 10 BLAH10
5 BLAH5 9 BLAH9
但是只有三個固定級別,再次加入更加容易理解...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.