分級查詢應該可以工作。由於你想從每個PID的最高SEQ開始,所以需要一些額外的技巧。
SELECT pid, fulldesc FROM (
SELECT pid, SYS_CONNECT_BY_PATH(desc, '||') fulldesc, seq, minseq FROM (
SELECT pid, seq, desc,
MAX(seq) OVER (PARTITION BY pid) maxseq,
MIN(seq) OVER (PARTITION BY pid) minseq
FROM tableB
)
START WITH seq = maxseq
CONNECT BY pid = PRIOR pid AND seq = PRIOR seq - 1
)
WHERE seq = minseq
ORDER BY pid
;
編輯:添加一個過濾器一種方式的要求,在註釋:
SELECT pid, fulldesc FROM (
SELECT pid, SYS_CONNECT_BY_PATH(desc, '||') fulldesc, seq, minseq FROM (
SELECT pid, seq, desc,
MAX(seq) OVER (PARTITION BY pid) maxseq,
MIN(seq) OVER (PARTITION BY pid) minseq
FROM tableB
WHERE pid IN (SELECT pid FROM tableB WHERE desc='day.')
)
START WITH seq = maxseq
CONNECT BY pid = PRIOR pid AND seq = PRIOR seq - 1
)
WHERE seq = minseq
ORDER BY pid
http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:2196162600402#18313422264397 – 2011-03-08 04:14:20
該示例使用函數! – jagamot 2011-03-08 04:20:40