博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【leetcode】Department Top Three Salaries
阅读量:7190 次
发布时间:2019-06-29

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

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 ;

 

转载于:https://www.cnblogs.com/seyjs/p/5129206.html

你可能感兴趣的文章
搭建前端监控系统(备选)用户行为统计和监控篇(如何快速定位线上问题)...
查看>>
linux常用命令
查看>>
python获取系统时间
查看>>
frame与bounds的区别比较
查看>>
<正则吃饺子> :关于使用pd创建表时需要注意的地方
查看>>
Python输入和输出
查看>>
GIL(全局解释器锁)
查看>>
sqlserver 计算数据库时间差
查看>>
SQL 存储过程使用
查看>>
Gradle 配置国内镜像
查看>>
php实现排列组合
查看>>
Hibernate入门第二课 Hibernate Tools for Eclipse Plugins安装
查看>>
Redis配置文件详解
查看>>
python学习day4之路文件的序列化和反序列化
查看>>
ArrayList和LinkedList区别及性能测试
查看>>
高精度模板
查看>>
mysql5.7 多级主从+multisource
查看>>
linux 查看文件夹大小 du命令
查看>>
Web前端性能优化之反向代理
查看>>
linux中cron用法
查看>>