Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Nth maximum value 1

Status
Not open for further replies.

spradeepraj

Programmer
Apr 13, 2001
32
0
0
IN
How to find out the Nth maximum value of a Numeric Column. mail me : spradeepraj@dsqsoft.com
Delphidhasan
 
Hi,
AFAIK you can't directly...
If you mean how to get the
10th highest value ( for instance) you would need to use PL/Sql code and loop thru the values to determine that.

[profile]
 
I put it in here so that other people could see it.

select NumericColumn from(
select rownum Ordinal, NumericColumn from Table order by NumericColumn desc)
where ordinal = <Your Number>;

Hope this helps
Mark
 
PS - the Numeric Column has to be indexed for this to work (I think)
Mark
 
Hi,
I tried your solution on some data I have and it produces an incorrect result--Did I miss something?

Code:
  1  select cmptn_rt from (select rownum Ordinal,cmptn_rt from hr_cur_rptng
  2   order by cmptn_rt desc)
  3* where ordinal = 10
SQL> /

  CMPTN_RT
----------
     19.02

SQL>

That is nowhere near the 10th highest cmptn_rt.
( cmptn_rt is an indexed field)

[profile]
 
I think the problem with Custom24's suggestion is that the rownum in the subselect is determined before the sort. That means selecting the tenth &quot;ordinal&quot; doesn't correspond to the tenth highest value.

If are willing to select the top ten values, you can do this with one subselect. Otherwise you will probably need two.

To get the top 10:

select NumericColumn from(
select NumericColumn from Table order by NumericColumn desc)
where rownum <= 10;

To get the tenth highest:

select NumericColumn from (
(select NumericColumn from(
select NumericColumn from Table order by NumericColumn desc)
where rownum <= 10;
order by NumericColumn)
where rownum = 1;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top