【问题标题】:want to find starting and end point of non-sequential number in oracle stored procedure想在oracle存储过程中找到非连续数的起点和终点
【发布时间】:2020-05-14 19:30:14
【问题描述】:

假设主表是

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

【问题讨论】:

  • 太好了。你有什么尝试?
  • 我想要使用 oracle 存储过程的结果,但无法得到它
  • 为什么“使用oracle存储过程”?自然的方式是“通过使用标准 SQL 查询”。单独的问题:您的 Oracle 数据库版本是什么(例如,11.2.0.4 或 12.1.0.2 等)?如果您不知道,请运行 select banner from v$version 并查看它的内容。

标签: oracle oracle12c gaps-and-islands match-recognize


【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-03
    • 2017-08-25
    • 2021-09-19
    • 2011-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-01
    相关资源
    最近更新 更多