Solved

Generate a sequence based on Alphabetical order:

Posted on 2014-02-12
5
393 Views
Last Modified: 2014-02-16
Hi,
i have data as below

CODE

421
BD
AA
AA
CD
KK
MM

i Want to create sequence based on Alphabetical order

CODE   seq

421   1
AA    2
AA    2
BD    3
CD    4
KK    5
MM  6


Thanks
0
Comment
Question by:sam2929
  • 2
  • 2
5 Comments
 
LVL 74

Expert Comment

by:sdstuber
ID: 39855078
SELECT code, ROW_NUMBER() OVER (ORDER BY code) rn
  FROM yourdata;


or

SELECT code, ROWNUM
  FROM (SELECT code
          FROM yourdata
        ORDER BY code);
0
 
LVL 28

Assisted Solution

by:Naveen Kumar
Naveen Kumar earned 250 total points
ID: 39855100
try this :

select x.code, x.rr
from xyz y, ( select code, rownum rr
from (
select code
from xyz
group by code
order by code ) ) x
where x.code = y.code
0
 
LVL 28

Expert Comment

by:Naveen Kumar
ID: 39855105
If the queries given by sdstuber did not give you the exact output you are looking for, then my query should give the output you are looking for.  my query will give 2 for both AA code records where as the sdstuber queries will give 2 and 3 for the AA code records.

Thanks,
0
 
LVL 74

Accepted Solution

by:
sdstuber earned 250 total points
ID: 39855112
oh I didn't even notice the repeat in the results.

But that's simple enough, just change row_number to dense_rank in the first post


SELECT code, DENSE_RANK() OVER (ORDER BY code) rn
  FROM yourdata;


this way you don't need to double query your table,  it's just a sort.
0
 
LVL 32

Expert Comment

by:awking00
ID: 39857457
Can there be lowercase letters involved?
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
This video shows how to recover a database from a user managed backup

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question