【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 ...
随机推荐
- Oracle 看出表结构与属性、表空间设计
1.Oracle 查看表空间 SELECT b.comments as 注释, a.column_name as 列名, a.data_type || '(' || a.data_length || ...
- 【神经网络与深度学习】【C/C++】ZLIB学习2
Zlib文件压缩和解压 开源代码:http://www.zlib.net/ zlib使用手册:http://www.zlib.net/manual.html zlib wince版:http://ww ...
- Oracle网络相关概念与常用配置文件
监听器(Listener) 监听器是Oracle基于服务端的一种网络服务,主要用于监听客户端向数据库服务器提出的链接请求. 本地服务名(Tnsname) Oracle客户端与服务器端的链接是通过客户端 ...
- sublime集成MinGW,打造C/C++开发环境
MinGW是是将GCC编译器和GNU Binutils移植到Win32平台下的产物,包括一系列头文件(Win32API).库和可执行文件.MinGW是从Cygwin(1.3.3版)基础上发展而来.GC ...
- 双元素非递增(容斥)--Number Of Permutations Educational Codeforces Round 71 (Rated for Div. 2)
题意:https://codeforc.es/contest/1207/problem/D n个元素,每个元素有a.b两个属性,问你n个元素的a序列和b序列有多少种排序方法使他们不同时非递减(不同时g ...
- a标签的download属性
a标签加上downlaod属性后,就可完成对href属性链接文件的下载,但仅仅是限于同源文件,如果是非同源,download属性会失效. 无download属性的时候,a标签的默认行为是链接跳转进行预 ...
- java在遍历列表的时候删除列表中某个元素
在遍历list的时候需要删除其中的某些元素,不要用foreach遍历,需要用Iterator. List<String> list = new ArrayList<String> ...
- java常用的加密技术
详见:https://blog.csdn.net/it_beecoder/article/details/71480770 Base64是网络上最常见的用于传输8Bit字节代码的编码方式之一,大家可以 ...
- httpd统计的其他方法,awk,sed等
1. https://stackoverflow.com/questions/345546/how-to-get-requests-per-second-for-apache-in-linux 2. ...
- 小米Air安装Arch Linux之图形界面配置(Gnome 和 sway)持续更新中……
0. 前言 上一篇文章简单讲述了在小米Air上安装Arch Linux的经验,但是安装完后基本系统后,还需要额外的配置才能进到日常使用.下文简单列举一些步骤. 1. 参考网站 主要还是参考ARCH W ...