根据biillno进行分组后根据LV排序后再进行前后比对,求出a1连续递增、连续递减、连续不减少的最大值~然后在把比对出来的值插入另一张表中。

解决方案 »

  1.   

    @yaiger
    @nayi_224
      

  2.   

    你可以参考https://bbs.csdn.net/topics/392420616nayi_224的回答,自己尝试写一下
      

  3.   

    加个partition就行了with tab as 
    (select 1 id, t1.a1, 1 lv from temp01 t1 union all
    select 1 id, t1.a2, 2 from temp01 t1 union all
    select 1 id, t1.a3, 3 from temp01 t1 union all
    select 1 id, t1.a4, 4 from temp01 t1 union all
    select 1 id, t1.a5, 5 from temp01 t1 union all
    select 2 id, t1.a2, 2 from temp01 t1 union all
    select 2 id, t1.a3, 3 from temp01 t1 union all
    select 2 id, t1.a4, 4 from temp01 t1 union all
    select 1 id, t1.a6, 6 from temp01 t1 )
    ,tab2 as (
    select t1.*,
           case
             when lead(t1.a1) over(partition by id order by lv) > a1 and
                  nvl(lag(t1.a1) over(partition by id order by lv), a1 + 1) > a1 then
              1
             when lag(t1.a1) over(partition by id order by lv) >= a1 then
              1
             else
              0
           end fin
      from tab t1)
    , tab3 as (
    select t1.*, sum(t1.fin) over(partition by id order by lv) su from tab2 t1
    ), tab4 as (
    select t1.*, 
           count(1) over(partition by id, su) part_1_cot
      from tab3 t1
    )
    select t1.*,
           max(t1.part_1_cot) over(partition by id) part_2_cot
      from tab4 t1;