首页 > oracle中如何处理结果集合并?

oracle中如何处理结果集合并?

我现在查询出来的结果集如上图所示。我想在oracle查询的结果集中,重新将startNo和endNo连续的票号给合并起来。例如我圈起来的那个段票号为:
01016680~01018000和010119937~010200000

这个应该怎么去实现?


假设表的名字叫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
【热门文章】
【热门文章】