What is the simplest way to get the nth highest value from a result set using plain SQL?
The result set would be huge, thus need to consider performance too.>
You have to incur the overhead of sorting, and in my example rownum
is the sorted row number, not the physical location.
Since everyone is using analytic functions to show how this works here is one:
select foo,bar, max(baz)
from
(
select *
from
(
select foo,bar,baz, row_number() over
(partition by some_identifier_that_Groups order by value DESC) rn
)
where rn = 1 -- get the highest value for each partition
) group by foo,bar