create table hoge(
NO number(1),
区分 number(1),
連番A number(1),
連番B number(1));
insert into hoge values(1,1,null,null);
insert into hoge values(2,2,null,null);
insert into hoge values(3,2,null,null);
insert into hoge values(4,3,null,null);
insert into hoge values(5,1,null,null);
insert into hoge values(6,2,null,null);
insert into hoge values(7,2,null,null);
insert into hoge values(8,2,null,null);
insert into hoge values(9,3,null,null);
commit;
--■■■mergeを使わない方法■■■
update hoge a set (連番A,連番B) =
(select b.rn1,b.rn2
from (select c.no,c.rn1,row_number() over (partition by c.rn1 order by c.no) as rn2
from (select d.no,sum(decode(d.区分,1,1,0)) over(order by d.no) as rn1
from hoge d) c) b
where b.no = a.no);
--■■■mergeを使う方法(10g以降)■■■
merge into hoge a
using (select c.no,c.rn1,
row_number() over (partition by c.rn1 order by c.no) as rn2
from (select d.no,
sum(decode(d.区分,1,1,0)) over(order by d.no) as rn1
from hoge d) c) b
on (a.no=b.no)
when matched then
update set a.連番A=b.rn1,
a.連番B=b.rn2;