Solved

Generate a sequence based on Alphabetical order:

Posted on 2014-02-12
5
395 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
[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

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

Working with Network Access Control Lists in Oracle 11g (part 1) Part 2: http://www.e-e.com/A_9074.html So, you upgraded to a shiny new 11g database and all of a sudden every program that used UTL_MAIL, UTL_SMTP, UTL_TCP, UTL_HTTP or any oth…
Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
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 videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

739 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