2013-07-11 46 views
0

我將嘗試解釋表的佈置方式,以便我需要的可能會更清晰一些。用於查找非空列值的第一個實例的Oracle SQL遞歸

############################################################### 
# cid # iid # child cid # child iid # target cid # target iid # 
############################################################### 
# 112 # 1 # null  # null  # 116  # 1   # 
# 112 # 2 # 112  # 1   # null  # null  # 
# 112 # 3 # 112  # 1   # 116  # 2   # 
# 112 # 4 # 112  # 1   # 100  # 3   # 
# 112 # 101 # null  # null  # 116  # 101  # 
# 112 # 102 # 112  # 101  # null  # null  # 
# 112 # 103 # 112  # 101  # 116  # 102  # 
# 112 # 201 # null  # null  # 116  # 201  # 
# 112 # 202 # 112  # 201  # null  # null  # 
# 112 # 203 # 112  # 201  # 116  # 202  # 
# 112 # 301 # null  # null  # 116  # 301  # 
# 112 # 302 # 112  # 301  # null  # null  # 
# 112 # 302 # 112  # 301  # 116  # 302  # 

上面是我試圖從中獲取數據的表格的減少表示形式。對不起,如果佈局是有點廢話。這裏的每一行都是一個對象。這些對象中的每一個都可以有子對象,例如,第一行沒有子對象,但鏈接到目標對象。第二行有一個子對象,並且沒有鏈接到目標對象,但是,它通過具有目標對象的子cid和iid鏈接回第1行。第三行也與第一行鏈接,但它也有一個目標對象,所以我實際上並不想回到第一行。

其他表

######################################### 
# cid # iid # col1 # col2 # col3 # col4 # 
######################################### 
# 116 # 1 # a # null # 16 # 1 # 
# 116 # 2 # b # 1 # 6 # null # 
# 116 # 3 # n # 1 # 11 # 2 # 
# 116 # 101 # n # 2 # 61 # 3 # 
# 116 # 102 # b # null # 161 # 101 # 
# 116 # 201 # a # 33 # 312 # 116 # 
# 116 # 202 # a # 33 # 312 # 116 # 
# 116 # 301 # s # 56 # 1321 # 33 # 
# 116 # 302 # r # 6 # 22 # 12 # 

得到的表

########################################################################################### 
# cid # iid # child cid # child iid # target cid # target iid # col1 # col2 # col3 # col4 # 
########################################################################################### 
# 112 # 1 # null  # null  # 116  # 1   # a # null # 16 # 1 # 
# 112 # 2 # 112  # 1   # null  # null  # a # null # 16 # 1 # 
# 112 # 3 # 112  # 1   # 116  # 2   # b # 1 # 6 # null # 
# 112 # 4 # 112  # 1   # 100  # 3   # n # 1 # 11 # 2 # 
# 112 # 101 # null  # null  # 116  # 101  # n # 2 # 61 # 3 # 
# 112 # 102 # 112  # 101  # null  # null  # n # 2 # 61 # 3 # 
# 112 # 103 # 112  # 101  # 116  # 102  # b # null # 161 # 101 # 
# 112 # 201 # null  # null  # 116  # 201  # a # 33 # 312 # 116 # 
# 112 # 202 # 112  # 201  # null  # null  # a # 33 # 312 # 116 # 
# 112 # 203 # 112  # 201  # 116  # 202  # a # 33 # 312 # 116 # 
# 112 # 301 # null  # null  # 116  # 301  # s # 56 # 1321 # 33 # 
# 112 # 302 # 112  # 301  # null  # null  # s # 56 # 1321 # 33 # 
# 112 # 302 # 112  # 301  # 116  # 302  # r # 6 # 22 # 12 # 

[只是爲了澄清,在第一表中,目標CID和IID涉及CID和IID在另一個表中即時通訊鏈接到它。 ]

本質上我需要的是遞歸回去的表,直到行有一個目標對象引用。

如果一行有一個孩子的c /我的id和一個目標c /我的id我只想要目標c /我的id。

任何人都可以指向正確的方向嗎?

我正在通過 http://docs.oracle.com/cd/B19306_01/server.102/b14200/queries003.htm慢慢閱讀,但我發現它有點混亂。我不會成爲更簡單的SQL查詢的專家,所以遞歸在我的腦海中有點過分了。

感謝

編輯:其他表和結果

+0

什麼是預期的結果數據? – haki

+0

基本上,我想鏈接另一個表到這個。這個想法是,目標id應該匹配另一個表。我需要上表中的每一行都有一個對目標iid的引用,所以我可以將其他表中的相關數據鏈接到它。 – Predz

+0

顯示兩個表格+數據和預期結果。 – haki

回答

3

我不知道你到底需要什麼樣的補充例子,但 你可以用tihs statment開始

select cid, iid, level, connect_by_root(target_cid), connect_by_root(target_iid) 
from tab 
connect by prior cid = child_cid 
      AND prior iid = child_iid 
      AND target_cid is null   
; 

,然後filtern的你需要的條目

select * 
from 
(
select cid, iid, level, connect_by_root(target_cid) as target_cid, connect_by_root(target_iid) as target_iid 
from tab 
connect by prior cid = child_cid 
      AND prior iid = child_iid 
      AND target_cid is null   
) 
where target_cid is not null 
;   

    CID IID TARGET_CID TARGET_IID 
    ++++++++++++++++++++++++++++++ 
    112 1  116   1 
    112 2  116   1 
    112 3  116   2 
    112 4  100   3 
    112 101 116   101 
    112 102 116   101 
    112 103 116   102 
    112 201 116   201 
    112 202 116   201 
    112 203 116   202 
    112 301 116   301 
    112 302 116   301 
    112 302 116   302 
+0

偉大的起點,幫助我解決了類似的問題。據我所知,而不是過濾(嵌套查詢和'where target_cid不是空值'),可以寫入'start with target_cid is not null connect by ...',而不用嵌套查詢來獲取只有「target_cid」的非空結果(但我只在非常有限的數據集上進行了測試) – grek40

相關問題