使用Oracle 12C使用match_recognize查找损坏的序列

时间:2017-07-26 08:02:11

标签: database oracle

我想检测每个数字的CDR_ID序列中的空白,因为我正在使用match_recognize分析函数。

代码:

SQL> select  distinct number,
             cdr_id,
             status_sequence
      from    TMP_CDR_GAPS  match_recognize (
                        partition by number 
                        order     by cdr_id
                        measures  match_number() status_sequence
                        all rows per match
                        after match skip past last row
                        pattern   (section_start in_seq_value*)
                        define    in_seq_value as (cdr_id = prev(cdr_id))
                        )

这是我得到的结果:

NUMBER          CDR_ID      STATUS_SEQUENCE                         
003301011849    536         1
003301011849    537         2
003301011849    538         3
003301011849    539         4
003301011849    540         5
003301011849    541         6
003301011849    542         7
003301011849    544         8           <-- !!! 
003301011849    545         9
003301011849    546         10
003301011849    547         11
003301011849    548         12

你会注意到CDR_ID 543缺失,这正是我想要检测的,所以我期待STATUS_SEQUENCE重新启动为1,用于下一个CDR_ID(在这种情况下为CDR_ID 544)。

有人可以帮我理解match_recognize语法的错误吗?

先谢谢你, 大卫

1 个答案:

答案 0 :(得分:2)

相互堆叠一些分析......

select num, cdr_id,
    row_number() over (partition by num, sequence_group order by cdr_id) as status_sequence
from tmp_cdr_gaps
    match_recognize (
        partition by num
        order     by cdr_id
        measures  match_number() sequence_group
        all rows per match
        after match skip past last row
        pattern   (section_start in_seq_value*)
        define    in_seq_value as (cdr_id = prev(cdr_id)+1)
    );

稍微修改一下你的查询和一个额外的分析......

match_recognize

最后,仅使用select * from tmp_cdr_gaps match_recognize ( partition by num order by cdr_id measures count(1) as status_sequence all rows per match after match skip past last row pattern (sequence_group in_sequence*) define in_sequence as (cdr_id = prev(cdr_id)+1) ); ...

match_recognize()

这似乎有效,因为,正如我发现in Oracle docin_seq_value中的聚合函数具有执行运行聚合的默认操作方式。

也谢谢你让我更接近掌握这个对SQL的精彩补充。 : - )

关于你的有人可以帮我理解match_recognize语法的错误吗? ...

有很多错误:

  1. 您的cdr_id = prev(cdr_id)定义为cdr_id没有意义。根据此定义,您说要识别相同cdr_id值的序列,而实际上您希望识别match_number() status_sequence值的序列增加1。
  2. 您的cdr_id = prev(cdr_id)个数字按顺序找到每个模式。对于cdr_id = prev(cdr_id)+1,此类模式将匹配12个单行“子组”,每个子组按顺序编号。 1 cdr_id536 542 2cdr_id 544 548 {{1}}行{{1}} {{1}} {1}}至{{1}}。