Fill a column with serial number

Hello, all seniors... !

my table has a column empty NWT (number), I would now like to fill with a series of numbers
for as many records that are also present.

examples of table data.
  1  SELECT  TXN_DATE
  2  FROM PTXN
  3* WHERE ROWNUM <10

TXN_DATE
----------
18/03/2010
02/02/2010
10/02/2010
05/04/2010
15/05/2010
25/01/2010
03/02/2010
15/01/2010
25/04/2010

9 rows selected.

I issue this command;
update ptxn
set tno = rownum

With the above Update SQL, the TNO column got filled with the exact number of records. Like this,
SELECT  TXN_DATE,tno
FROM PTXN;

TXN_DATE         TNO
---------- ----------
03/02/2010          1
15/01/2010          2
02/02/2010          4
25/04/2010          8
25/01/2010          3
18/03/2010          6
15/05/2010          9
10/02/2010          5
05/04/2010          7

9 rows selected.
what I want actually is to sort my table (ptxn) in the column "txn_date" wise and complete series.
Like this;
TXN_DATE NWT
---------- ----------
15/01/2010 1
25/01/2010 2
02/02/2010 4
02/03/2010 8
02/10/2010 3
2010-03-18 6
2010-05-04 9
2010-04-25 5
2010-05-15 7


How can I change my statement Update ot any other solution, please suggest.

Using PL/SQL:

SQL> select * from PTXN ;

TXN_DATE         TNO
--------- ----------
07-MAR-10
26-FEB-10
01-MAR-10
08-FEB-10
03-MAR-10
16-APR-10
11-APR-10
08-MAY-10
14-MAR-10
18-APR-10

10 rows selected.

SQL> begin
  2   for r in (select rowid i, TXN_DATE, row_number() over (order by TXN_DATE) n from PTXN) loop
  3     UPDATE PTXN p
  4        set TNO = r.n
  5      where rowid = r.i;
  6   end loop;
  7  end;
  8  /

PL/SQL procedure successfully completed.

SQL> select * from PTXN
  2  order by 1;

TXN_DATE         TNO
--------- ----------
08-FEB-10          1
26-FEB-10          2
01-MAR-10          3
03-MAR-10          4
07-MAR-10          5
14-MAR-10          6
11-APR-10          7
16-APR-10          8
18-APR-10          9
08-MAY-10         10

10 rows selected.

Max
[My Italian blog Oracle | http://oracleitalia.wordpress.com/2010/01/31/le-direttive-di-compilazione-pragma/]

Tags: Database

Similar Questions

Maybe you are looking for