【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 ...
随机推荐
- 前端 api 请求缓存方案
参考链接:https://blog.csdn.net/zhuoganliwanjin/article/details/89598753#commentBox
- [c++] 链表各类操作详解
链表概述 链表是一种常见的重要的数据结构.它是动态地进行存储分配的一种结构.它可以根据需要开辟内存单元.链表有一个“头指针”变量,以head表示,它存放一个地址.该地址指向一个元素.链表中每一个元素称 ...
- PTA(Basic Level)1021.个位数统计
给定一个 k 位整数 \(N=d_{k−1}10^{k−1}+⋯+d_110^1+d_0 (0≤d_i≤9, i=0,⋯,k−1, d_{k−1}>0)\),请编写程序统计每种不同的个位数字出现 ...
- CentOS7下MySQL服务启动失败原因及解决方法
在重启阿里的CentOS7服务器后,重启MySQL 出现错误 Starting mysqld (via systemctl): Job for mysqld.service failed becaus ...
- Linux软链接硬链接的区别
ln是linux中又一个非常重要命令,它的功能是为某一个文件在另外一个位置建立一个同步的链接.当我们需要在不同的目录,用到相同的文件时,我们不需要在每一个需要的目录下都放一个必须相同的文件,我们只要在 ...
- HDU 6662 Acesrc and Travel 换根DP,宇宙最傻记录
#include<bits/stdc++.h> typedef long long ll; using namespace std; const int maxn=1e6+50; cons ...
- Python 条件判断 和循环
使用条件判断 if else # 条件派单 if else print('条件派单 if else') # s = input('请输入生日年号:') # birth = int(s) birth = ...
- Go项目目录管理
在Go的官网文档How to Write Go Code中,已经介绍了Go的项目目录一般包含以下几个: src 包含项目的源代码文件: pkg 包含编译后生成的包/库文件: bin 包含编译后生成的可 ...
- dedecms 列表 用分页标签 判断 当第一页则显示,第二页以上不显示 土办法!
arc.listview.class.php function GetPageListST($list_len,$listitem="index,end,pre,next,pageno,sp ...
- 抄来的,占个位儿【百度架构师是怎样搭建MySQL分布式集群】
1.准备集群搭建环境 使用6台虚拟机来搭建MySQL分布式集群,相应的实验环境与对应的MySQL节点之间的对应关系如下图所示: 管理节点(MGM):这类节点的作用是管理MySQLCluster ...