Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Generate a sequence based on Alphabetical order:

Posted on 2014-02-12
Medium Priority
398 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
Question by:sam2929
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• Learn & ask questions
• 2
• 2
5 Comments

LVL 74

Expert Comment

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

Naveen Kumar earned 1000 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

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

sdstuber earned 1000 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

ID: 39857457
Can there be lowercase letters involved?
0

## Featured Post

Question has a verified solution.

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

This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
###### Suggested Courses
Course of the Month6 days, 5 hours left to enroll

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

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