Wednesday, May 11, 2011

Get the value which is before the MAX value (Use of RANK function)



When come to a scenario where we want to get the value which is just before the MAX value we cant use rownum.It will work for only 1st row.
But we can use RANK function to rank the ordered values and then select the desired rank.In this case rank 2.


Eg:



select UserID,Value
from (select UserID,Value,
      rank() over (partition by UserID order by Value Desc) RNK
      from t)
where rnk=2

No comments:

Post a Comment