Solved

Generate a sequence based on Alphabetical order:

Posted on 2014-02-12
5
384 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 73

Expert Comment

by:sdstuber
Comment Utility
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
Comment Utility
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
Comment Utility
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 73

Accepted Solution

by:
sdstuber earned 250 total points
Comment Utility
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 31

Expert Comment

by:awking00
Comment Utility
Can there be lowercase letters involved?
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Update table using multiple table qualifications 12 36
EXECUTE IMMEDIATE 5 34
PAYER_ID has both atributes 4 19
dates - loop 12 40
Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now