Group by values that are in sequence

前端 未结 4 1866
谎友^
谎友^ 2020-12-05 16:14

I have some table like this

row chequeNo
 1     15
 2     19
 3     20
 4     35
 5     16

and I need to get the result like this



        
4条回答
  •  星月不相逢
    2020-12-05 16:22

    You can use Aketi Jyuuzou's technique called Tabibitosan here:

    SQL> create table mytable (id,chequeno)
      2  as
      3  select 1, 15 from dual union all
      4  select 2, 19 from dual union all
      5  select 3, 20 from dual union all
      6  select 4, 35 from dual union all
      7  select 5, 16 from dual
      8  /
    
    Table created.
    
    SQL> with tabibitosan as
      2  ( select chequeno
      3         , chequeno - row_number() over (order by chequeno) grp
      4      from mytable
      5  )
      6  select row_number() over (order by grp) "row"
      7       , min(chequeno) "from"
      8       , max(chequeno) "to"
      9    from tabibitosan
     10   group by grp
     11  /
    
           row       from         to
    ---------- ---------- ----------
             1         15         16
             2         19         20
             3         35         35
    
    3 rows selected.
    

    Regards,
    Rob.

提交回复
热议问题