How to count consecutive duplicates in a table?

前端 未结 6 1959
闹比i
闹比i 2021-01-13 09:52

I have below question:
Want to find the consecutive duplicates

SLNO   NAME     PG   
1       A1      NO                   
2       A2      YES                    


        
6条回答
  •  不知归路
    2021-01-13 10:12

    Try to use row_number()

    select
        SLNO,
        Name,
        PG,
        row_number() over (partition by PG order by PG) as 'Consecutive'
    from
        
    order by
        SLNO,
        NAME,
        PG
    

    This is should work with minor tweaking.

    --EDIT--

    Sorry, partiton by PG. The partitioning tells the row_number when to start a new sequence.

    提交回复
    热议问题