문제

I want to add a sequence number (SEQNO) based on their REFNO and STARTD.

Old Table

New Table

도움이 되었습니까?

해결책

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)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 dba.stackexchange
scroll top