0

假设主表是

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

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

喜欢

Start  End
1      3
6      7
4

1 回答 1

1

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

with子句仅用于模拟您的输入数据;您应该删除它,并在主查询 ( 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
于 2020-05-14T19:46:18.190 回答