How can I find duplicate consecutive values in this table?

后端 未结 3 471
旧巷少年郎
旧巷少年郎 2020-12-16 03:52

Say I have a table which I query like so:

select date, value from mytable order by date

and this gives me results:

date             


        
3条回答
  •  一个人的身影
    2020-12-16 04:50

    Use the lead and lag analytic functions.

    create table t3 (d number, v number);
    insert into t3(d, v) values(1, 1);
    insert into t3(d, v) values(2, 2);
    insert into t3(d, v) values(3, 2);
    insert into t3(d, v) values(4, 3);
    insert into t3(d, v) values(5, 2);
    insert into t3(d, v) values(6, 3);
    insert into t3(d, v) values(7, 4);
    
    select d, v, case when v in (prev, next) then '*' end match, prev, next from (
      select
        d,
        v,
        lag(v, 1) over (order by d) prev,
        lead(v, 1) over (order by d) next
      from
        t3
    )
    order by
      d
    ;
    

    Matching neighbours are marked with * in the match column,

提交回复
热议问题