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

RSS Syndication


Edi Yanto
Oracle Applications Technical Consultant
View Edi Yanto's profile on LinkedIn
Edi Yanto (何 萬 新) - Blogged Page Rank Check

My Certifications


  • Edi Yanto: Hi Ang, You can find the solution for this problem at http://www.ediyanto.co.cc/unable-to-view-excel-output-xml-publisher-from-concurrent-program-req
  • ang: xml view output to excel ORA-01403: no data found -------------------------------------------------------------- FRM-40735: WHEN-BUTTON-PRESSED
  • Edi Yanto: Hi Ricky, Have you try to set the security setting (Enable Automatic prompting for file download) for your internet/local/trusted sites zone at inter

RSS Edi Yanto (何万新)

  • Oracle Top-N Query October 2, 2009
    Using RANK function, with a faster execution plan (WINDOW SORT PUSHED RANK) SELECT Empno, Ename, Job, Mgr, Hiredate, Sal FROM (SELECT Empno, Ename, Job, Mgr, Hiredate, Sal, RANK() OVER (ORDER BY SAL Desc) AS Emp_Rank FROM Emp) WHERE Emp_Rank
    Edi Yanto
  • Bersyukurlah… June 30, 2009
    AKU TAK SELALU MENDAPATKAN APA YANG KUSUKAI oleh karena itu AKU SELALU MENYUKAI APAPUN YANG AKU DAPATKAN. Kata-kata di atas merupakan wujud syukur. Syukur merupakan kualitas hati yang terpenting. Dengan bersyukur kita akan senantiasa diliputi rasa damai, tentram dan bahagia. Sebaliknya, perasaan tak bersyukur akan senantiasa membebani kita. Kita akan selalu […]
    Edi Yanto
  • Cinta Sejati June 30, 2009
    Kenapa kita menutup mata ketika kita tidur ? Kenapa kita menutup mata ketika kita menangis ? Kenapa kita menutup mata ketika kita membayangkan sesuatu ? Kenapa kita menutup mata ketika kita berciuman ? Hal hal yang terindah di dunia ini biasanya tidak terlihat Ada hal hal yang tidak ingin kita lepaskan dan ada orang orang yang tidak ingin kita tinggalkan Tap […]
    Edi Yanto
  • Nice Story June 8, 2009
    Share on Facebook
    Edi Yanto
  • 5 Tips Agar Tak Mudah Lelah June 2, 2009
    Sebagian orang mengeluhkan kondisi tubuhnya yang cepat capai atau lelah. Kebanyakan orang menilai hal itu diakibatkan kerja ekstra. Tetapi, sebenarnya hal itu dilatarbelakangi kebiasaan seseorang sehari-hari. Berikut lima tips yang dikutip VIVAnews dari WebMD, Sabtu 25 April 2009 agar kondisi tubuh Anda tidak mudah capai atau lelah. 1. Lapar atau tidak, sara […]
    Edi Yanto
  • Belajar dari Alphabet May 29, 2009
    A : AcceptTerimalah diri anda sebagaimana adanya. B : BelievePercayalah terhadap kemampuan anda untuk meraih apa yang anda inginkan dalam hidup. C : CarePedulilah pada kemampuan anda meraih apa yang anda inginkan dalam hidup. D : DirectArahkan pikiran pada hal-hal positif yang meningkatkan kepercayaan diri. E : EarnTerimalah penghargaan yang diberi orang lai […]
    Edi Yanto
  • How To Get the List of All Responsibilities Assigned To A User May 29, 2009
    Edi Yanto
  • Query To Find Multi-Org Is Enabled Or Not May 29, 2009
    SELECT DECODE (multi_org_flag, ‘Y’, ‘Multi-Org Enabled’, ‘Multi-Org Disabled’ ) FROM fnd_product_groups; Share on Facebook
    Edi Yanto
  • How To Find A Patch Is Applied For A Bug May 29, 2009
    Edi Yanto
  • Excel Limitations on Images in Oracle Apps May 23, 2009
    The Excel output can’t showing the image (even after you save and send to another person or open it at different notebook), but it is showing only small red color empty cross written ‘An Image’.  Why??? As many of you know or have noticed, our Excel output for all flavors of BIP/XMLP is not true binary [...]
    Edi Yanto

 

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

Visitors

Blog Stats

  • 27,872 hits

Users Online

web counter

Pages