【问题标题】:Oracle: Is it possible to filter out duplicates only when they appear in succession?Oracle:是否可以仅在重复出现时才过滤掉它们?
【发布时间】:2020-12-01 11:39:26
【问题描述】:

提前感谢您的帮助。

我有一张表格,里面有司机的行程信息。有时行程似乎有相同的站点(但相隔几天)。我希望能够查询该表并过滤掉地址相同且日期连续的任何记录。

这可能吗?

再次感谢, 乔什

【问题讨论】:

    标签: oracle duplicates


    【解决方案1】:
    with tst as(
        select timestamp '2020-08-01 00:00:00' dt, '123 street' loc from dual
        union all
        select timestamp '2020-08-01 00:00:00', '89 street' from dual
        union all
        select timestamp '2020-08-02 00:00:00', '456 airport' from dual
        union all
        select timestamp '2020-08-04 00:00:00', '456 airport' from dual
        union all
        select timestamp '2020-08-05 00:00:00', '67 street' from dual
        union all
        select timestamp '2020-08-06 00:00:00', '89 street' from dual
        union all
        select timestamp '2020-08-07 00:00:00', '123 street' from dual
    )
    select dt, loc
    from (
        select dt, loc, nvl(lag(loc) over(order by dt), 'FIRST_ROW') prev_loc
        from tst
    ) where loc <> prev_loc;
    

    fiddle

    【讨论】:

      【解决方案2】:

      另一种方法是使用Tabibitosan method,它为连续的行分配一个组号,然后计算每个组的行数。(在 asktom 网站上找到)。

      with test_data as(
          select date'2020-08-01' dt, '123 street' loc from dual
          union all
          select date '2020-08-01', '89 street' from dual
          union all
          select date '2020-08-02', '456 airport' from dual
          union all
          select date '2020-08-04', '456 airport' from dual
          union all
          select date '2020-08-05', '67 street' from dual
          union all
          select date '2020-08-06', '89 street' from dual
          union all
          select date '2020-08-07', '123 street' from dual
      )
      select max(dt),loc
      from
      (
      select t.*
        ,row_number() over (order by dt) -
         row_number() over (partition by loc order by dt) grp
        from test_data t
      )
      group by grp,loc
      having count(*) > 1;
      

      另一种使用 match_recognize 的方法从 12c 开始可用。使用 {1,} 的模式表示重复一次或多次

      了解更多 match_recognize here

      with test_data as(
          select date'2020-08-01' dt, '123 street' loc from dual
          union all
          select date '2020-08-01', '89 street' from dual
          union all
          select date '2020-08-02', '456 airport' from dual
          union all
          select date '2020-08-04', '456 airport' from dual
          union all
          select date '2020-08-05', '67 street' from dual
          union all
          select date '2020-08-06', '89 street' from dual
          union all
          select date '2020-08-07', '123 street' from dual
      )
      select * 
        from test_data
      match_recognize (
        order by dt
        all rows per match
        pattern (equal{1,})
        define 
          equal as loc = prev(loc)
      );
      

      游乐场:Dbfiddle

      【讨论】:

        猜你喜欢
        • 2022-08-22
        • 2020-12-30
        • 2021-12-22
        • 1970-01-01
        • 2020-03-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多