Oracle SQL:根据行重新启动序列

Oracle SQL: Restart sequence based on row

我的数据集如下:

**series**
start
continue
continue
end
start
continue
end

我需要添加一个包含序列 ('leeftijd_seq') 的列,该序列从 'start' 开始,在 'continue' 增加 1,在 'end' 结束。我试过下面的代码,但我的执行语句对我创建的序列不起作用。

SELECT batchnr, series,
  CASE WHEN series = 'start' THEN execute 'alter sequence leeftijd_seq restart start with 1;' and leeftijd_seq.nextval
    when series = 'continue' then leeftijd_seq.nextval
    ELSE leeftijd_seq.currval
    END SEQ_NO
FROM data order by batchnr asc

这看起来像是一个缺口和孤岛问题。我建议使用 window 个“start”来构建组:

select t.*,
    row_number() over(partition by grp order by batchnr) as seq_no
from (
    select t.*,
        sum(case when series = 'start' then 1 else 0 end) over(order by batchnr) as grp
    from mytable t
) t