想在oracle存储过程中找到非序号的起点和终点

want to find starting and end point of non-sequential number in oracle stored procedure

假设主要 table 是

ID  Usage_flah
1     null
2     null
3     null
4     Yes
5     Yes
6     Null
7     NUll

现在我想要第二个结果中 ID 的开始和结束位置 table 其中 usage_flag 为空

喜欢

Start  End
1      3
6      7

这是一种方法(说明为什么知道您的 Oracle 版本是相关的:此解决方案使用 match_recognize,在 Oracle 12.1 中引入)

with子句只是为了模拟你输入的数据;你应该删除它,并在主查询中使用你实际的 table 和列名 (select * .......)

with
  main_table (id, usage_flag) as (
    select 1, null  from dual union all
    select 2, null  from dual union all
    select 3, null  from dual union all
    select 4, 'Yes' from dual union all
    select 5, 'Yes' from dual union all
    select 6, null  from dual union all
    select 7, null  from dual
  )
select *
from   main_table
match_recognize (
  order by id
  measures first(id) as id_start, last(id) as id_end
  pattern  ( n+ )
  define   n as usage_flag is null
);

  ID_START     ID_END
---------- ----------
         1          3
         6          7