如何从MS SQL中的表中单独获得最高“N'th Maximum”值?

时间:2009-04-15 10:57:26

标签: sql sql-server

假设有一个'Employee'表,其中salary是其中一列。

我希望从表中获得“最高N'的最高薪水。

如何轻松获取此内容?

6 个答案:

答案 0 :(得分:3)

SELECT TOP 1 employee.name, employee.salary from (
    SELECT TOP N employee.name, employee.salary
    FROM employee
    ORDER BY employee.salary DESC )

这使得Nth从顶部开始。

答案 1 :(得分:2)

WITH    (
        SELECT  e.*, ROW_NUMBER() OVER (ORDER BY employee.salary DESC) AS rn
        FROM    employee
        ) AS q
SELECT  *
FROM    q
WHERE   rn = @n

答案 2 :(得分:1)

SELECT TOP 1 E.Salary
FROM (SELECT TOP(N) Salary
      FROM Employee
      ORDER BY Salary DESC) E
ORDER BY E.Salary

答案 3 :(得分:0)

SELECT TOP N employee.name, employee.salary
FROM employee
ORDER BY employee.salary DESC

答案 4 :(得分:0)

SELECT TOP 1 Salary FROM
(SELECT TOP(N) Salary FROM Employee
ORDER BY Salary DESC) E

答案 5 :(得分:-1)

 SELECT TOP 1 * FROM Employees
    ORDER BY Employees.Salary DESC