且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

对递增 1 的连续行进行分组

更新时间:2023-11-26 10:18:58

一个常见的解决方案是利用 ROW_NUMBER 和您的序列中存在间隙的事实:

A common solution utilizes a ROW_NUMBER and the fact that there are gaps in your sequence:

select FirstKey,
   SecondKey,
   min(Sequence),
   max(Sequence)
from 
 (
   select FirstKey,
      SecondKey,
      Sequence,
      -- meaningless value, but the same for sequential rows
      Sequence -
      row_number() over (partition by FirstKey, SecondKey order by Sequence) as grp 
   from tab
 ) dt
group by 
   FirstKey,
   SecondKey,
   grp