2013-10-31 56 views
0

記錄被保存爲與pairkey列鏈接的同一個表中的父母子女,然後甚至父母被分成與scndleg列鏈接的兩條腿。也就是說,將有兩條父母腿與scndleg列相鏈接,並且每個父母將在pairkey列中具有父母的seqnno父母的子女。選擇記錄作爲配對父母子女

看這個fiddle

,我需要選擇一個完整的批次爲

Both legs of interlinked parents 
UNION 
All children of these two parents 

,然後其他批次以下相同的模式等在fiddle

回答

0

編輯新所示查詢版本(鏈接到SQLFiddle末尾):

SELECT 
    seqnno, 
    narration, 
    pairkey, 
    scndleg 
    FROM (
    SELECT p.*, LEAST(seqnno, scndleg) related_leg_min_id 
     FROM pairs p 
) 
START WITH scndleg IS NOT NULL 
CONNECT BY pairkey = PRIOR seqnno AND scndleg IS NULL 
ORDER BY connect_by_root(related_leg_min_id), scndleg DESC NULLS LAST, pairkey 
; 

輸出:

 SEQNNO NARRATION     PAIRKEY SCNDLEG 
---------- ------------------------ ---------- ---------- 
     1 1st leg parent     1   4 
     4 2nd leg parent     4   1 
     2 1st leg child      1    
     3 1st leg child      1    
     5 2nd leg child      4    
     6 2nd leg child      4    
     7 another 1st leg parent   7   10 
     10 another 2nd leg parent   10   7 
     8 another 1st leg child    7    
     9 another 1st leg child    7    
     11 another 2nd leg child   10    
     12 another 2nd leg child   10  

SQLFiddle

+0

否,1日2次腿家長會在一起,然後他們的孩子,然後又第1和第2條腿的父母和他們的孩子... – bjan

+0

@bjan然後你能提供你期望的樣本輸出嗎?因爲我無法根據你的描述找出它...... –

+0

在查詢面板中有一個查詢小提琴,運行並查看結果 – bjan

相關問題