Pergunta

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

Old Table

New Table

Foi útil?

Solução

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.

Outras dicas

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)

Licenciado em: CC-BY-SA com atribuição
Não afiliado a dba.stackexchange
scroll top