Oracle - Convert value from rows into ranges

前端 未结 3 1647
攒了一身酷
攒了一身酷 2021-01-06 06:23

Are there any techniques that would allow a row set like this

WITH 
base AS
(
    SELECT  1 N FROM DUAL UNION ALL
    SELECT  2 N FROM DUAL UNION ALL
    SEL         


        
3条回答
  •  清歌不尽
    2021-01-06 06:29

    Yet another way:

    with base as (
        select  1 n from dual union all
        select  2 n from dual union all
        select  3 n from dual union all
        select  6 n from dual union all
        select  7 n from dual union all
        select 17 n from dual union all
        select 18 n from dual union all
        select 19 n from dual union all
        select 21 n from dual)
    select a,b 
    from (select a
                ,case when b is not null and a is not null
                      then b
                      else lead(n) over (order by n)
                end b
          from (select n
                      ,a
                      ,b
                from (select n
                            ,case n-1 when lag (n) over (order by n) then null else n end a
                            ,case n+1 when lead (n) over (order by n) then null else n end b
                      from base)
          where a is not null
             or b is not null))
    where a is not null
    order by a
    

提交回复
热议问题