0

I have a table which contains 69000 records. Now, how to add a new column(say index) which should contain the value 1 for 1st row, 2 for 2nd row ....etc.

I want the following result

   id      name      index
 --------------------------
 9796896   sandy       1
 796869    raj         2
rajashekar
  • 449
  • 2
  • 16

3 Answers3

2

Add the column and update it with something like that :

with cte as
(
   select *
       , rank() over (order by id desc) as ranker
   from test
)
update test
   set index = ranker
from cte
where cte.id = test.id;
Jean-François Fabre
  • 131,796
  • 23
  • 122
  • 195
Jaisus
  • 919
  • 3
  • 14
0

Try this:

ALTER TABLE Tablename
    ADD COLUMN index int               
        GENERATED BY DEFAULT AS IDENTITY;
Ankit Das
  • 587
  • 3
  • 14
0

you can use identity column of the oracle:

alter table your_Table add index_ number GENERATED by default as IDENTITY;

also, you can add the column and then assign value to it:

alter table your_Table add index_ number;

update your_Table tt
set tt.index_  =  (select rn from (
select row_number() over (order by id desc) rn
from your_Table t
) t where t.id = tt.id)

Cheers!!

Popeye
  • 34,995
  • 4
  • 9
  • 31