How to get nᵗʰ highest value using plain SQL

前端 未结 8 1074
孤城傲影
孤城傲影 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:08

    In a general database, you can do this with a subquery and two order bys. The problem is that top/limit/rownum syntax is not standard. In some databases you would write:

    0 讨论(0)
  • 2021-01-25 00:11

    Sub Query will list out top 'n' highest salary values. From that list minimum value will be the nth highest salary.

    `SELECT min(salary) FROM
        (SELECT DISTINCT TOP n salary FROM EmployeeTable ORDER BY salary desc);`
    

    Eg :- SQL query to find third maximum salary

    `SELECT min(salary) FROM
        (SELECT DISTINCT TOP 3 salary FROM EmployeeTable ORDER BY salary desc);`
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2021-01-25 00:20

    In Oracle:

    SELECT * FROM (
      SELECT col1, ROW_NUMBER()OVER(ORDER BY col1) rnum_col1 FROM table1
    ) WHERE rnum_col1 = 10;
    
    0 讨论(0)
  • 2021-01-25 00:20

    Let's say you want to find the 5th highest salary, you first take the first 5 distinct salary order by descending order so that the last one is the 5th highest salary (See inner query). You then reorder it in ascending order and take the first.

    SELECT TOP 1 Salary FROM
    (
      SELECT DISTINCT TOP 5 Salary
      FROM Employee
      ORDER BY Salary DESC)  t
    
    ORDER BY  Salary ASC
    
    0 讨论(0)
  • 2021-01-25 00:20

    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
    
    0 讨论(0)
提交回复
热议问题