Home » SQL & PL/SQL » SQL & PL/SQL » Any SQL GURU -- Interesting SQL Problem
Any SQL GURU -- Interesting SQL Problem [message #20162] Thu, 02 May 2002 13:46 Go to next message
sql gurus
Messages: 22
Registered: May 2002
Junior Member
I have a coulmn which has repeated values. i want to write a sql to number the values from 1..n for each value.
Ex.
Column A
-------
10
10
10
40
30
30
30

the result should be
Column A Number
------- -------
10 1
10 2
10 3
40 1
30 1
30 2
30 3

Any thoughts appreciated.
Re: Any SQL GURU -- Interesting SQL Problem [message #20164 is a reply to message #20162] Thu, 02 May 2002 14:38 Go to previous messageGo to next message
Todd Barry
Messages: 4819
Registered: August 2001
Senior Member
Jay, if there's a chance you don't really need these numbers to be persistant, you can just assign them on the fly using analytical functions (assuming you are 8i or later):

select c, row_number() over (partition by c order by c) rn 
  from t;


If the row numbers absolutely have to be stored, then you can:

update t
   set s = (select rn
              from (select rowid, c, row_number() over (partition by c order by c) rn
                      from t) t2
             where t2.c = t.c
               and t2.rowid = t.rowid);


where s is the name of the "numbering" column.

If you are not on 8i, then you will probably need to use a PL/SQL routine to loop through the table and do the numbering assignments.
Re: Any SQL GURU -- Interesting SQL Problem [message #20171 is a reply to message #20162] Fri, 03 May 2002 06:22 Go to previous messageGo to next message
sql gurus
Messages: 22
Registered: May 2002
Junior Member
Thank you very much Todd. It works
Thanks [message #20174 is a reply to message #20162] Fri, 03 May 2002 06:38 Go to previous message
sql gurus
Messages: 22
Registered: May 2002
Junior Member
Thanks Todd. It works.
Previous Topic: Trigger Mutation Problem
Next Topic: I would like to FORCE Oracle to show me what it doesn't want to show me
Goto Forum:
  


Current Time: Fri Apr 19 03:34:38 CDT 2024