編輯新所示查詢版本(鏈接到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
否,1日2次腿家長會在一起,然後他們的孩子,然後又第1和第2條腿的父母和他們的孩子... – bjan
@bjan然後你能提供你期望的樣本輸出嗎?因爲我無法根據你的描述找出它...... –
在查詢面板中有一個查詢小提琴,運行並查看結果 – bjan