Consider the following table create table table1(a number); insert into table1 values(5); insert into table1 values(23); insert into table1 values(8); insert into table1 values(99); insert into table1 values(99); commit; To retrieve the Nth largest number : ======================== Lets say we want to find the 2nd largest number 1)There are two interpretations for the this. When we consider the distinct values , then 23 is the second largest number. So to find the absolute second largest number ,the following query can be used : select a from ( select a , rank() over (order by a desc) rank from (select distinct(a) from table1) ) where rank=2; 2) If we are concerned about true ranking ie equal values have equal ranks then the following query can be used:(In this case there will be no rank 2) select a from (select a, rank() over (order by a desc) rk from table1) where rk=2; Cheers Vinodh
sanjith
Jan 24th, 2007
try this outselect distinct(a.C.N) from T.N a where &n=(select count(b.C.N) from T.N b where a.C.N <= b.C.N)C.N == Column nameT.N == Table namea & b == alias name&n == nth largest element
SHEEL2006
Feb 6th, 2007
selecta.salary from <tabalename a>
where &n=( selectcount(distinct b.salary)
from <table name b> where a.salary<=b.salary;
this quarry gives u 1st,2nd,3rd,4th............................ higest salary but here I use only copy of 1st table in statement selectcount(distinct b.salary)from <tablename b) but one more interestingthing is that u can also find the 1st,2nd,3rd.............. min salary also with this quarry
selecta.salary from <tabalename a> where &n=( selectcount(distinct b.salary)from <table name b>where a.salary<b.salary;
donot use topN-analysis
Radhi
Feb 15th, 2007
if u need nth row in a table try this---select rownum,ename,eno from emp group by rownum,ename,eno having rownum>=nminusselect rownum,ename,eno from emp group by rownum,ename,eno having rownum>n/
Consider the table-->
table (a number);
select a
from
(
select a rank() over (order by a desc) rank
from
select distinct (a) from table
)
where rank 1;
select * from ( select rownum n, size_query.* from ( select scalar_column from the_table order by scalar_column desc ) size_query ) where n = 2 ;
...you can also apply this to row counts...
select * from ( select rownum n, count_query.* from ( select identifier, count(*) the_count from some_table group by identifier order by count(*) desc ) count_query ) where n = 2 ;
How to write a SQL query to find N'th largest element in a Column???
Profile Answers by bkkemparaju Questions by bkkemparaju
Questions by bkkemparaju