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的更多相关文章

  1. 【LeetCode】692. Top K Frequent Words 解题报告(Python)

    [LeetCode]692. Top K Frequent Words 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/top ...

  2. 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 ...

  3. 【leetcode】347. Top K Frequent Elements

    题目地址:https://leetcode.com/problems/top-k-frequent-elements/ 从一个数组中求解出现次数最多的k个元素,本质是top k问题,用堆排序解决. 关 ...

  4. 【LeetCode】347. Top K Frequent Elements 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 解题方法 字典 优先级队列 日期 题目地址:https://l ...

  5. 【LeetCode】代码模板,刷题必会

    目录 二分查找 排序的写法 BFS的写法 DFS的写法 回溯法 树 递归 迭代 前序遍历 中序遍历 后序遍历 构建完全二叉树 并查集 前缀树 图遍历 Dijkstra算法 Floyd-Warshall ...

  6. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  7. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  8. 【LeetCode】36. Valid Sudoku 解题报告(Python)

    [LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...

  9. 【LeetCode】895. Maximum Frequency Stack 解题报告(Python)

    [LeetCode]895. Maximum Frequency Stack 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxueming ...

随机推荐

  1. 华为HCNA乱学Round 5:华为交换机基础

  2. 关于虚拟机docker 启动mysql 启动成功但未挂载到端口

    首先排查了防火墙和其他权限相关问题 然后检查了mysql 用户权限问题 docker logs 查看日志 正常应该是到3306 问题是我的mysql my.cnf 文件是挂在在本地.当第二次启动容器时 ...

  3. oracle在group by时某列有多个值的拼接

    最近编码过程中出现了group by后,某些列会有多个值,而我需要把这些多个值的列进行拼接的情况,和大家分享一下. 有如下表student: 我们希望以class分组,每组的信息平铺,效果如下 分组首 ...

  4. JavaScript中好用的对象数组去重

    对象数组去重 Demo数据如下: var items= [{ "specItems": [{ "id": "966480614728069122&qu ...

  5. 在.Net中使用RedLock实现分布式锁

    ⒈简介 RedLock 分布式锁算法由 Redis 的作者提出,大部分语言都有对应的实现,查看,RedLock.net 是 RedLock 分布式锁算法的 .NET 版实现,用来解决分布式下的并发问题 ...

  6. python中进程的几种创建方式

    在新创建的子进程中,会把父进程的所有信息复制一份,它们之间的数据互不影响. 使用os.fork()创建 该方式只能用于Unix/Linux操作系统中,在windows不能用. import os # ...

  7. Java后端技术面试汇总(第一套)

    面试汇总,整理一波,doc文档可点击[此处下载] 1.基础篇 1.1.Java基础 • 面向对象的特征:继承.封装和多态• final, finally, finalize 的区别• Exceptio ...

  8. mybaits 在test判断数字,或者数字型字符串时注意事项

    1.在test中判断传入值为0的Integer或者Long时,mybaits会将其视为null 解决方法: 把Integer/Long改为String类型. status!=null and stat ...

  9. EBS描述性弹性域及键弹性域总结

    一.描述性弹性域 前言介绍: 描述性弹性域的实质就是系统预留自定字段,系统可以使用说明性弹性域来获取业务所特有的重要附加信息.系统可能自定义说明性弹性域,以显示存储更多信息的字段,提供一套完整的“自定 ...

  10. Delphi RadioGroup 组件