寻找第N高薪

时间:2018-12-03 09:18:08

标签: sql sql-server sql-server-2012

编写SQL查询以从Employee表(SQL Server)中获取第n个最高工资

| Id | Salary |
+----+--------+
| 1  | 100    |
| 2  | 200    |
| 3  | 300    |

在此示例中,n = 2的第n个最高薪水是200。如果没有n个最高薪水,则查询应返回null。

| getNthHighestSalary(2) |
+------------------------+
| 200                    |

除了使用函数以外,还有什么其他方式可以编写此查询?

8 个答案:

答案 0 :(得分:5)

另一种可能的方法是使用ROW_NUMBER()或DENSE_RANK()函数。重要的是要知道在第N个职位之前或之前是否可以有一个以上的薪水。

CREATE TABLE #Salary (
    [id] int,
    [salary] numeric(10, 2)
)

INSERT #Salary 
    ([id], [salary])
VALUES
    (1, 100),
    (2, 500),
    (3, 200),
    (4, 300),
    (5, 200);

DECLARE @Position int = 2;

-- With possible duplicate salaries
WITH cte AS (
    SELECT 
        [id], [salary],
        DENSE_RANK() OVER (ORDER BY [salary] ASC) AS [DRPosition]
    FROM #Salary
)
SELECT [id]
FROM cte
WHERE [DRPosition] = @Position
ORDER BY [DRPosition];

-- Without possible duplicate salaries
WITH cte AS (
    SELECT 
        [id], [salary],
        ROW_NUMBER() OVER (ORDER BY [salary] ASC) AS [RPosition]
    FROM #Salary
)
SELECT [id]
FROM cte
WHERE [RPosition] = @Position
ORDER BY [RPosition]

答案 1 :(得分:2)

The following does almost exactly what you want:

select salary
from employee
order by salary desc
offset <n> rows fetch next 1 row only;

The only problem is that it does not return NULL when there is no such salary. You can handle using a subquery:

select (select salary
        from employee
        order by salary desc
        offset <n> rows fetch next 1 row only
       ) as salary;

If you want ties to have the same ranking, then use select distinct salary in the subquery.

答案 2 :(得分:1)

您可以尝试以此获得第n个最高薪水,其中n = 1,2,3 ....(int)

SELECT TOP 1 salary FROM (
   SELECT TOP n salary 
   FROM employees 
   ORDER BY salary DESC) AS emp 
ORDER BY salary ASC

希望这会对您有所帮助。下面是实现之一。

create table #salary (salary int)
insert into #salary values (100), (200), (300), (400), (500)

SELECT TOP 1 salary FROM (
   SELECT TOP 3 salary 
   FROM #salary 
   ORDER BY salary DESC) AS emp 
ORDER BY salary ASC

drop table #salary

这里的输出是300,因为500是第一高的,400是第二高的,300是第三高的,如下图所示

salary
300

这里n是3

答案 3 :(得分:1)

您可以使用row_number()

尝试此操作
WITH CTE AS
(
    SELECT EmpID, Salary,
           RN = ROW_NUMBER() OVER (ORDER BY Salary DESC)
    FROM Employee
)
SELECT EmpID, Salary
FROM CTE
WHERE RN = n

答案 4 :(得分:1)

查找恰好有8个较高薪水的工资,
意思是找到第9位最高工资
如果没有第9个薪水,则返回NULL

SELECT DISTINCT e.Salary FROM Employee e WHERE
(SELECT COUNT(*) FROM Employee ie WHERE ie.Salary > e.Salary) = 8
UNION 
SELECT NULL WHERE (SELECT COUNT(Salary) FROM Employee) < 9

答案 5 :(得分:1)

您可以简单地使用相关子查询来尝试此操作,以在Employee表中找到Nth最高薪水。

SELECT *
FROM Employee Emp1
WHERE (N-1) = (
SELECT COUNT(DISTINCT(Emp2.Salary))
FROM Employee Emp2
WHERE Emp2.Salary > Emp1.Salary)

答案 6 :(得分:0)

我试过这个查询并且成功

static class OffsetAndLag {
    public long offset;
    public long lag;
    public OffsetAndLag(long offset, long lag) {
        this.offset = offset;
        this.lag = lag;
    }
}

答案 7 :(得分:0)

177。 Nth 最高薪水 - Leetcode

试试这个!成功了!

CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN 
SET N = N-1;
    RETURN (
    SELECT DISTINCT Salary as "getNthHighestSalary(2)"
    FROM Employee
    ORDER BY Salary DESC
    LIMIT 1 OFFSET N
  
  );
END