December 9, 2004
Oracle Top-N Queries
In Oracle8i, selecting the top-N query is sometimes very useful. In order to do it, first use an inline view to obtain all data in a sorted order. Then utilize the imaginary row numbering column, rownum, to determine the top number of rows to output.
For example:
select account_no, discharge_date, rownum
from (select * from encounter order by discharge_date)
where rownum <=10;
An important distinction to make is that you do not need to use inline views if you do not have an ORDER BY clause in your query. If not, you can simply write:
select account_no, discharge_date,
from encounter
where rownum <= 10
Further, even if you did have an ORDER BY clause and hence needed to use an inline view, you don't need to reference rownum in the parent select query:
select account_no, discharge_date
from (select * from encounter order by discharge_date)
where rownum <=10;
This will return the same rows. And lastly, it is usually vastly more efficient to only select the columns you'll use in the parent SELECT clause in your inline view:
select account_no, discharge_date
from (select account_no, discharge_date from encounter order by discharge_date)
where rownum <=10;
I make these points because the ability to use this sort of functionality at the database level rather than at the ColdFusion level can significantly improve the performance of your applications.
Posted by: Dave Carabetta at December 9, 2004 1:00 PMThanks Elyse for the tip.
Regarding Dave Carabetta's comments from December 9, 2004, these comments are largely useless.
#1: >you do not need to use inline views if you do not have an ORDER BY clause in your query
Dave may be right, but this would seem to be useless. "Top n" queries only makes sense if you use order by. Otherwise you are getting the "top n" records of an undefined record order, which seldom would be useful.
#2: > you don't need to reference rownum in the parent select query
Dave then goes on to reference the rownum in the parent query. Gotcha.
#3: Dave posted his comments on my 15th wedding anniversary.
Finally passed the test
Managing in light of McGregor's Theory X and Theory Y
CMMI
Kicking HIT Leadership Up a Notch
That's just some mumbo jumbo project management BS
Outcomes - The tactic to get to the strategy
Nurse Call, VOIP, and Wi-Fi: Its just cool when things come together!
December 2007
November 2007
October 2007
September 2007
August 2007
July 2007
June 2007
May 2007
April 2007
March 2007
February 2007
January 2007
December 2006
November 2006
August 2006
June 2006
May 2006
April 2006
March 2006
February 2006
January 2006
November 2005
October 2005
September 2005
August 2005
June 2005
May 2005
April 2005
March 2005
February 2005
January 2005
December 2004
November 2004
October 2004
September 2004
August 2004
July 2004
June 2004
May 2004
April 2004
March 2004
February 2004
January 2004
December 2003
November 2003
October 2003
Joel on Software
David Ross
Edward Prevost
Martin Fowler
The Health Care Blog
The Tales of Hoffman
The Business Word
Medical Rants
Christina's Considerations
Paul Levy
HIS Talk
Appropriate IT
Candid CIO
RSS feed




