博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode] Department Top Three Salaries 系里前三高薪水
阅读量:6624 次
发布时间:2019-06-25

本文共 2770 字,大约阅读时间需要 9 分钟。

The Employee table holds all employees. Every employee has an Id, and there is also a column for the department Id.

+----+-------+--------+--------------+| Id | Name  | Salary | DepartmentId |+----+-------+--------+--------------+| 1  | Joe   | 70000  | 1            || 2  | Henry | 80000  | 2            || 3  | Sam   | 60000  | 2            || 4  | Max   | 90000  | 1            || 5  | Janet | 69000  | 1            || 6  | Randy | 85000  | 1            |+----+-------+--------+--------------+

The Department table holds all departments of the company.

+----+----------+| Id | Name     |+----+----------+| 1  | IT       || 2  | Sales    |+----+----------+

Write a SQL query to find employees who earn the top three salaries in each of the department. For the above tables, your SQL query should return the following rows.

+------------+----------+--------+| Department | Employee | Salary |+------------+----------+--------+| IT         | Max      | 90000  || IT         | Randy    | 85000  || IT         | Joe      | 70000  || Sales      | Henry    | 80000  || Sales      | Sam      | 60000  |+------------+----------+--------+

这道题是之前那道的拓展,难度标记为Hard,还是蛮有难度的一道题,综合了前面很多题的知识点,首先看使用Select Count(Distinct)的方法,我们内交Employee和Department两张表,然后我们找出比当前薪水高的最多只能有两个,那么前三高的都能被取出来了,参见代码如下:

解法一:

SELECT d.Name AS Department, e.Name AS Employee, e.Salary FROM Employee eJOIN Department d on e.DepartmentId = d.IdWHERE (SELECT COUNT(DISTINCT Salary) FROM Employee WHERE Salary > e.SalaryAND DepartmentId = d.Id) < 3 ORDER BY d.Name, e.Salary DESC;

下面这种方法将上面方法中的<3换成了IN (0, 1, 2),是一样的效果:

解法二:

SELECT d.Name AS Department, e.Name AS Employee, e.Salary FROM Employee e, Department dWHERE (SELECT COUNT(DISTINCT Salary) FROM Employee WHERE Salary > e.SalaryAND DepartmentId = d.Id) IN (0, 1, 2) AND e.DepartmentId = d.Id ORDER BY d.Name, e.Salary DESC;

或者我们也可以使用Group by Having Count(Distinct ..) 关键字来做:

解法三:

SELECT d.Name AS Department, e.Name AS Employee, e.Salary FROM (SELECT e1.Name, e1.Salary, e1.DepartmentId FROM Employee e1 JOIN Employee e2 ON e1.DepartmentId = e2.DepartmentId AND e1.Salary <= e2.Salary GROUP BY e1.Id HAVING COUNT(DISTINCT e2.Salary) <= 3) e JOIN Department d ON e.DepartmentId = d.Id ORDER BY d.Name, e.Salary DESC;

下面这种方法略微复杂一些,用到了变量,跟中的解法三使用的方法一样,目的是为了给每个人都按照薪水的高低增加一个rank,最后返回rank值小于等于3的项即可,参见代码如下:

解法四:

SELECT d.Name AS Department, e.Name AS Employee, e.Salary FROM (SELECT Name, Salary, DepartmentId,@rank := IF(@pre_d = DepartmentId, @rank + (@pre_s <> Salary), 1) AS rank,@pre_d := DepartmentId, @pre_s := Salary FROM Employee, (SELECT @pre_d := -1, @pre_s := -1, @rank := 1) AS initORDER BY DepartmentId, Salary DESC) e JOIN Department d ON e.DepartmentId = d.IdWHERE e.rank <= 3 ORDER BY d.Name, e.Salary DESC;

本文转自博客园Grandyang的博客,原文链接:,如需转载请自行联系原博主。

你可能感兴趣的文章
Spring Boot五:使用properties配置文件实现多环境配置
查看>>
vim取消高亮显示
查看>>
设计从“心“开始
查看>>
windows7 系统盘 瘦身软件介绍: 冗余文件清理工具
查看>>
网络安全系列之四十六 在IIS6中配置目录安全性
查看>>
javascript理解数组和数字排序
查看>>
CocoStudio游戏发布后资源加密大致实现思路
查看>>
WPF SL 获取RichTextBox 的内容(string)
查看>>
微软同步框架入门之五--使用WCF同步远程数据
查看>>
Last-Modified、If-Modified-Since 实现缓存和 OutputCache 的区别
查看>>
漂亮彩色验证码 以及 数学运算表达式形式的验证码
查看>>
理解SQL代理错误日志
查看>>
维护计划作业
查看>>
Multipart Internet Mail Extensions (MIME)
查看>>
C# WinForm控件之Dock顺序调整
查看>>
中控科技 ZK Software的售后服务真像一坨屎,技术人员嚣张
查看>>
NSPredicate过滤数组数据
查看>>
设置MYSQL允许用IP访问
查看>>
spark 数据预处理 特征标准化 归一化模块
查看>>
大道至简,系统设计和模块划分的实用经验之谈
查看>>