我現在查詢出來的結果集如上圖所示。我想在oracle查詢的結果集中,重新將startNo和endNo連續的票號給合并起來。例如我圈起來的那個段票號為:01016680~01018000和010119937~010200000這個應該怎么去實現?
1 回答

翻過高山走不出你
TA貢獻1875條經驗 獲得超3個贊
假設表的名字叫T_CONNECT
select min(startno) || '~' || max(endno), count(*)
from (
-- 查出號碼連續記錄段的第一個號碼
select t.*, CONNECT_BY_ROOT(startno) rootno
from t_connect t
start with startno in (
--查出不連續號碼的第一條記錄,作為遞歸查詢的初始條件
select startno
from (
select startno, endno, lag(endno) over (order by startno) prev_endno
from t_connect t
order by startno
)
where (startno <> prev_endno + 1) or prev_endno is null
)
connect by prior endno + 1 = startno
)
group by rootno
order by rootno
- 1 回答
- 0 關注
- 1215 瀏覽
添加回答
舉報
0/150
提交
取消