How to get nᵗʰ highest value using plain SQL

前端 未结 8 1076
孤城傲影
孤城傲影 2021-01-24 23:42

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.

8条回答
  •  无人及你
    2021-01-25 00:18

    Start by producing an ordered, numbered dataset and then select from that. The precise syntax depends on the RDBMS but, for example, in Oracle you can do

    SELECT ROWNUM, SOMEVAL
    FROM (SELECT SOMEVAL FROM SOMETABLE ORDER BY SOMEVAL DESC)
    

    Given the above set you can

    SELECT SOMEVAL WHERE ROWNUM = :N
    

提交回复
热议问题