Adding sequence number to existing rows based on ID and Date in PLSQL Oracle [closed]
-
12-01-2021 - |
해결책
begin
For r in (Select t.*,
row_number() over (partition by refno order by startd) seqno
From my_table t
Order by refno, startd) loop
Update my_table
set seqno = r.seqno
where refno = r.refno
and startd = r.startd;
End loop;
Commit;
End;
See http://sqlfiddle.com/#!4/484b3/5 for example of row_number. This pl/sql anonymous block can only handle a limited number of records.
다른 팁
You can use MERGE
:
merge
into tableX t
using
( select t.*,
row_number() over (partition by refno order by startd) as rn
from tableX t
) r
on ( t.refno = r.refno and t.startd = r.startd )
when matched
then update
set
seqno = r.rn ;
Test in dbfiddle.uk.
(I assume that the PK is (refno, startd)
제휴하지 않습니다 dba.stackexchange