Question

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

Old Table

New Table

Était-ce utile?

La solution

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.

Autres conseils

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)

Licencié sous: CC-BY-SA avec attribution
Non affilié à dba.stackexchange
scroll top