17,086
社区成员
发帖
与我相关
我的任务
分享
with tab as (
select 1 id, 1 ord from dual union all
select 1 id, 2 ord from dual union all
select 2 id, 3 ord from dual union all
select 2 id, 4 ord from dual union all
select 1 id, 5 ord from dual union all
select 1 id, 6 ord from dual union all
select 3 id, 7 ord from dual union all
select 1 id, 8 ord from dual union all
select 3 id, 9 ord from dual
)
,tab2 as (
select t1.*,
decode(lag(t1.id) over(order by t1.ord), t1.id, 0, 1) lg
from tab t1
)
select t1.id,
t1.ord,
sum(t1.lg) over(partition by t1.id order by t1.ord) new_col
from tab2 t1
order by t1.ord
;