Home » SQL & PL/SQL » SQL & PL/SQL » Sequential Numbering
Sequential Numbering [message #1578] Tue, 14 May 2002 12:35 Go to next message
Susan Butts
Messages: 1
Registered: May 2002
Junior Member
We need help. I need to be able to have the database write a sequential number to a column based on an order by from another column. Example:

Country State Zip Members
US CA 72025 30
US TX 73041 51
US CA 75021 59
MX JL 3333 100

I will sort by Country and then by Members, then assign a sequential number to each of the rows within the country. Example of expected results:
RowCount Country State Zip Members
1 MX JL 3333 100
1 US CA 75021 59
2 US TX 73041 51
3 US CA 73025 30

How can I assign the RowCount as explained above.
Re: Sequential Numbering [message #1580 is a reply to message #1578] Tue, 14 May 2002 15:14 Go to previous messageGo to next message
Todd Barry
Messages: 4819
Registered: August 2001
Senior Member
I wouldn't recommend actually storing this number in the database (What happens when you update the member counts? You have to update all the numbering.)

Instead, use analytical functions to generate the sequential number within country at query time.

sql>select row_number() over (partition by country order by members desc) rn,
  2         country, state, zip, members
  3    from t;
 
       RN CO ST ZIP     MEMBERS
--------- -- -- ----- ---------
        1 MX JL 3333        100
        1 US CA 75021        59
        2 US TX 73041        51
        3 US CA 72025        30
Re: Sequential Numbering [message #1595 is a reply to message #1578] Wed, 15 May 2002 08:31 Go to previous messageGo to next message
Ramon Gallego
Messages: 1
Registered: May 2002
Junior Member
Todd,
first of all Thanks for your help...
We got an error after trying your suggestion. can you tell us what we are doing wrong...

here is our code..

SQL> select * from tc;

COUNT ST ZIP MEMBERS
----- -- ----- ---------
US CA 75025 300
US CA 73041 562
US MO 25254 600
US MO 25225 351
US MO 25222 700
US TX 75555 300
US TX 32500 300
US CA 73051 800

8 rows selected.

SQL> select row_number() over (partition by country order by members desc) rn,
2 country, state, zip, members
3 from tc;
select row_number() over (partition by country order by members desc) rn,
*
ERROR at line 1:
ORA-00919: invalid function
Re: Sequential Numbering [message #1648 is a reply to message #1595] Fri, 17 May 2002 08:00 Go to previous messageGo to next message
Todd Barry
Messages: 4819
Registered: August 2001
Senior Member
What version of Oracle are you using? You need to be using at least 8.1.6 to use analytical functions.
doubt regarding use of over [message #1735 is a reply to message #1578] Fri, 24 May 2002 13:56 Go to previous message
pavani
Messages: 32
Registered: April 2002
Member
hi can u pls tell me the use of over 'coz i never get through this in sql or pl/sql can u plss help me knowing this.when it is used .
thanks
Previous Topic: Job
Next Topic: Sorting a table in ascending ascii criteria
Goto Forum:
  


Current Time: Tue May 21 09:42:35 CDT 2024