【leetcode】Department Top Three Salaries
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级别,难点在于先组内排序,然后取组内前三的数据出来(排名可并列),最后再做一个组间排序。
因此,我的解题思路是这样的:
1.把数据按照DepartmentId,Salary 排序,这样的话同一部门的数据在搜索的结果集中就在一起。
select DepartmentId, Name ,Salary from Employee order by DepartmentId ,Salary desc
2.对同一部门的数据的记录进行编号,从1开始,如果和上一行的薪水相同,则编号和上一行一样。
select DepartmentId, Salary,Name,
@num := if(@cid = DepartmentId ,if(@cursalry != Salary, @num + 1,@num), 1) as number,
@maxprice := if(@num = 1 ,@maxprice := Salary,@maxprice) as mp,
@cid := DepartmentId as dummy,
@cursalry:= Salary as curs
from (select DepartmentId, Name ,Salary from Employee order by DepartmentId ,Salary desc) e ,(select @num := 0,@maxprice := 0,@cid := 0,@cursalry = 0) b
) c
3.过滤掉编号大于 3的记录。因为数据已经排序过,所以编号小于等于3的记录就是薪水的前三名。
select c.DepartmentId, c.Name ,c.Salary from
(
select DepartmentId, Salary,Name,
@num := if(@cid = DepartmentId ,if(@cursalry != Salary, @num + 1,@num), 1) as number,
@maxprice := if(@num = 1 ,@maxprice := Salary,@maxprice) as mp,
@cid := DepartmentId as dummy,
@cursalry:= Salary as curs
from (select DepartmentId, Name ,Salary from Employee order by DepartmentId ,Salary desc) e ,(select @num := 0,@maxprice := 0,@cid := 0,@cursalry = 0) b
) c where c.number <=3
) g
4.关联Department表,获取Department Name。
select f.Name as Department ,g.Name as Employee ,g.Salary from
(
select c.DepartmentId, c.Name ,c.Salary from
(
select DepartmentId, Salary,Name,
@num := if(@cid = DepartmentId ,if(@cursalry != Salary, @num + 1,@num), 1) as number,
@maxprice := if(@num = 1 ,@maxprice := Salary,@maxprice) as mp,
@cid := DepartmentId as dummy,
@cursalry:= Salary as curs
from (select DepartmentId, Name ,Salary from Employee order by DepartmentId ,Salary desc) e ,(select @num := 0,@maxprice := 0,@cid := 0,@cursalry = 0) b
) c where c.number <=3
) g ,Department f where g.DepartmentId = f.Id ;
【leetcode】Department Top Three Salaries的更多相关文章
- 【LeetCode】692. Top K Frequent Words 解题报告(Python)
[LeetCode]692. Top K Frequent Words 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/top ...
- LeetCode - 185. Department Top Three Salaries
The Employee table holds all employees. Every employee has an Id, and there is also a column for the ...
- 【leetcode】347. Top K Frequent Elements
题目地址:https://leetcode.com/problems/top-k-frequent-elements/ 从一个数组中求解出现次数最多的k个元素,本质是top k问题,用堆排序解决. 关 ...
- 【LeetCode】347. Top K Frequent Elements 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 解题方法 字典 优先级队列 日期 题目地址:https://l ...
- 【LeetCode】代码模板,刷题必会
目录 二分查找 排序的写法 BFS的写法 DFS的写法 回溯法 树 递归 迭代 前序遍历 中序遍历 后序遍历 构建完全二叉树 并查集 前缀树 图遍历 Dijkstra算法 Floyd-Warshall ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- 【LeetCode】36. Valid Sudoku 解题报告(Python)
[LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...
- 【LeetCode】895. Maximum Frequency Stack 解题报告(Python)
[LeetCode]895. Maximum Frequency Stack 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxueming ...
随机推荐
- C语言中typedef,条件编译,结构体的说明
目录 typedef (类型别名) 条件编译 条件编译在头文件包含中的应用 结构体 使用结构体定义新的结构体变量 结构体成员的引用与赋值 结构体指针及其引用 typedef (类型别名) typede ...
- 单机版的mysql安装
查是否安装了mysql:centos6:rpm -qa |grep mysqlcentos7:rpm -qa|grep mariadb或rpm -qa |grep mysql 有老的版本可以执行命令卸 ...
- [Python3] 036 函数式编程 返回函数
目录 函数式编程 之 返回函数 1. 引子 2. 闭包 closure 函数式编程 之 返回函数 函数可以返回具体的值 也可以返回一个函数作为结果 1. 引子 1.1 定义一个普通函数 >> ...
- 【Linux-驱动】将cdev加入到系统中去---cdev_add
在我们已经完成了对cdev结构体的初始化之后,我们需要将这个cdev结构体加入到系统中去,使用函数 cdev_add: /** * cdev_add() 讲一个字符设备加入到系统中去 * @p: 字符 ...
- HighGo瀚高数据库4.3版本安装说明
1. 通过与瀚高同事沟通, 获取到安装文件(点赞一下瀚高的同事, 效率很高并且说明的很完整) 瀚高是基于postgresql数据库做的深度定制开发的国产数据库. 不仅仅支持x86 也支持龙芯等全国产安 ...
- Websocket 突破最大长连接
为了测试机器能够最大的长连接个数,故写了一个js脚本,需要用node进行执行 var WebSocketClient = require('websocket').client; var size = ...
- 纯H5 AJAX文件上传加进度条功能
上传代码js部分 //包上传 $('.up_apk').change(function () { var obj = $(this); var form_data = new FormData(); ...
- 客户端相关知识学习(一)之混合开发,为什么要在App中使用H5页面以及应用场景、注意事项
混合开发 随着移动互联网的高速发展,常规的开发速度已经渐渐不能满足市场需求.原生H5混合开发应运而生,目前,市场上许多主流应用都有用到混合开发,例如支付宝.美团等.下面,结合我本人的开发经验,简单谈一 ...
- Redis之面试题总结
缓存雪崩 缓存穿透 缓存与数据库双写一致 最后 随着系统访问量的提高,复杂度的提升,响应性能成为一个重点的关注点.而缓存的使用成为一个重点.redis 作为缓存中间件的一个佼佼者,成为了面试必问项目. ...
- ueditor 后端配置项没有正常加载,上传插件不能正常使用 UTF8 PHP
修改config.json 文件,用DW软件打开,修改好后,保存 若用记事本打开的话修改后保存,无法加载