Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Generate a sequence based on Alphabetical order:

Posted on 2014-02-12
5
Medium Priority
?
399 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 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

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 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

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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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…
Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
Suggested Courses

885 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