第二高薪水

时间:2018-11-14 09:33:49

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

编写一个SQL查询以从Employee表中获取第二高的薪水。

spark.read.format("csv")
  .schema(schema)
  .option("header", false)
  .option("mode", "DROPMALFORMED")
  .option("maxMalformedLogPerPartition", 128)
  .load(inputCsvPath)

例如,给定上面的Employee表,查询应返回200作为第二高的薪水。如果没有第二高的薪水,则查询应返回null。

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

这是Leetcode的一个问题,我输入了以下代码:

    | SecondHighestSalary |
    | 200                 |

如果没有第二高薪的值,则查询不会返回NULL。  例如。如果表是

    SELECT CASE WHEN Salary = '' 
                THEN NULL
    ELSE Salary 
    END AS SecondHighestSalary 
    FROM (SELECT TOP 2 Salary
                ,ROW_NUMBER() OVER (ORDER BY Salary DESC) AS Num
          FROM Employee
          ORDER BY Salary DESC) AS T
    WHERE T.Num = 2

查询应返回

   | Id | Salary| 
   | 1  |  100  |

而不是

   |SecondHighestSalary|
   |       null        |

14 个答案:

答案 0 :(得分:2)

如果是平局,则需要第二高的唯一值。例如。对于100、200、300、300,则需要200。

因此获得最大值(MAX(salary) => 300),然后获得小于该最大值的最大值:

select max(salary) from mytable where salary < (select max(salary) from mytable);

答案 1 :(得分:1)

您应该能够使用OFFSET 1 / FETCH 1做到这一点

https://technet.microsoft.com/en-us/library/gg699618(v=sql.110).aspx

答案 2 :(得分:1)

您可以使用RANK()函数对Salary列的值进行排名。

SELECT *
FROM
(
 SELECT *, RANK()OVER(ORDER BY Salary DESC) As SalaryRank
 FROM Employee 
) AS Tab
WHERE SalaryRank = 2

答案 3 :(得分:1)

SELECT id, MAX(salary) AS salary 
FROM employee 
WHERE salary IN
(SELECT salary FROM employee MINUS SELECT MAX(salary) 
FROM employee); 

您可以尝试使用上面的代码找到第二个最高薪水。 上面的代码使用MINUS运算符。 有关更多参考,请使用以下链接 https://www.techonthenet.com/sql/minus.php https://www.geeksforgeeks.org/sql-query-to-find-second-largest-salary/

答案 4 :(得分:1)

Leetcode 工资第二高问题的解决方案是:

Select Max(Salary) AS SecondHighestSalary from Employee where Salary < (Select Max(Salary) from Employee);

答案 5 :(得分:0)

我将使用DENSE_RANK()并对雇员表执行LEFT JOIN

SELECT t.Seq, e.*
FROM ( VALUES (2) 
     ) t (Seq) LEFT JOIN
     (SELECT e.*,
             DENSE_RANK() OVER (ORDER BY Salary DESC) AS Num
      FROM Employee e
     ) e 
     ON e.Num = t.Seq;

答案 6 :(得分:0)

虽然您可以使用CTE(来自MSSQL 2005或更高版本)或ROWNUMBER,但最简单,更“便携”的方法是使用子查询进行两次订购。

select top 1 x.* from
(select top 2 t1.* from dbo.Employee t1 order by t1.Salary) as x
order by x.Salary desc

在没有第二个更大的薪水时显示null的要求比较棘手,但如果使用if则很容易。

if (select count(*) from dbo.Employee) > 1
begin
    select top 1 x.* from
    (select top 2 emp.* from dbo.Employee emp order by emp.Salary) as x
    order by x.Salary desc
end
else begin
    select null as Id, null as Salary
end

Obs: OP没有说第二大与第一并发时该怎么做,但是使用此解决方案只是在IF子查询中使用DISTINCT的简单问题。

答案 7 :(得分:0)

查询:

CREATE TABLE a
    ([Id] int, [Salary] int)
;

INSERT INTO a
    ([Id], [Salary])
VALUES
    (1, 100),
    (2, 200),
    (3, 300)
;

GO
SELECT Salary as SecondHighestSalary
FROM a 
ORDER BY Salary 
OFFSET 1 ROWS FETCH NEXT 1 ROWS ONLY
| SecondHighestSalary |
| ------------------: |
|                 200 |

答案 8 :(得分:0)

这是执行此操作的简单方法

SELECT MAX(Salary) FROM table WHERE Salary NOT IN (SELECT MAX(Salary) FROM table);

答案 9 :(得分:0)

您可以尝试以此获得第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)

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

drop table #salary

此处的输出为200,因为第一高的是300,第二高的是200,第三高的是100,如下所示

salary
200

这里n是2

答案 10 :(得分:0)

@media(max-width: 500px) {

}

答案 11 :(得分:0)

从员工限制 1,1 中选择工资;

很容易找到第二高的工资

答案 12 :(得分:0)

select
  case when max(salary) is null then null else max(salary) end SecondHighestSalary 
from (
  select salary , dense_rank() over (order by salary desc) as rn
  from Employee
)r
where rn = 2

This code returns null when there is no second highest salary

答案 13 :(得分:0)

您可以使用联合条件来处理空情况

SELECT Salary as "SecondHighestSalary" from Employee 
WHERE Salary < (SELECT MAX(salary) FROM Employee )    
UNION
(SELECT null)
ORDER BY 1 DESC
LIMIT 1;