Edi Yanto (何 萬 新)

Row Numbering With an Order By Clause

Posted by: Edi Yanto on: March 7, 2009


One of the most often uses of the pseudo column rownum is to provide sequence numbers to the records in a query. This feature is widely used in reports to represent systematic display of information.

SELECT   ROWNUM, rcta.trx_number, rcta.trx_date
FROM ra_customer_trx_all rcta;

ROWNUM    TRX_NUMBER    TRX_DATE
1    40881-05    12/14/2005
2    38384-05    11/24/2005
3    42740-05    12/28/2005
4    42742-05    12/28/2005
5    08883-07    3/12/2007
…………………………………..
…………………………………..
However, when we order this statement the rownum gets disturbed as shown below:

SELECT   ROWNUM, rcta.trx_number, rcta.trx_date
FROM ra_customer_trx_all rcta
ORDER BY rcta.trx_number;

ROWNUM    TRX_NUMBER    TRX_DATE
6427    00001-09    1/2/2009
10827    00001-09CM    1/6/2009
6361    00002-09    1/2/2009
10828    00002-09CM    1/6/2009
6248    00003-09    1/2/2009
……………………………………
……………………………………

As we can see from above the transaction number did get ordered but the rownum also got the wrong order. To achieve this we have to outer join this table with dual that process forces a implicit order on the rownum as shown below.

SELECT   ROWNUM, rcta.trx_number, rcta.trx_date
FROM ra_customer_trx_all rcta, DUAL d
WHERE rcta.trx_number = d.dummy(+)
ORDER BY rcta.trx_number;

ROWNUM    TRX_NUMBER    TRX_DATE
1    00001-09    1/2/2009
2    00001-09CM    1/6/2009
3    00002-09    1/2/2009
4    00002-09CM    1/6/2009
5    00003-09    1/2/2009
………………………………….
…………………………………

The trick is to do an outer join with the column that you want to order and this process does not disturb the rownum order. In addition to that if the column is of number datatype then one should make sure to use TO_CHAR datatype conversion function

Tags:

1 Response to "Row Numbering With an Order By Clause"

Hello!
Very Interesting post! Thank you for such interesting resource!
PS: Sorry for my bad english, I’v just started to learn this language ;)
See you!
Your, Raiul Baztepo

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

RSS Syndication


Edi Yanto
Oracle Applications Technical Consultant
View Edi Yanto's profile on LinkedIn

My Certifications




Enter your email address to follow this blog and receive notifications of new posts by email.

Join 992 other followers

Archives

 

March 2009
M T W T F S S
« Feb   Apr »
 1
2345678
9101112131415
16171819202122
23242526272829
3031  

Top Rated

RSS Unknown Feed

  • An error has occurred; the feed is probably down. Try again later.

Visitors

Categories

Blog Stats

  • 54,082 hits

Users Online

web counter

Pages

Follow

Get every new post delivered to your Inbox.

Join 992 other followers