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

本文共 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

你可能感兴趣的文章
【JAVASCRIPT】js知识点整理1
查看>>
两天入门五天掌握,这样的laravel别告诉我难
查看>>
老司机飙车GITC2016!金山混合云不只是获了个奖!
查看>>
PyTorch 1.0 正式发布,支持 eager 和 graph 模式无缝转换
查看>>
未来汽车横空出世,再不看你就OUT啦!
查看>>
李开复:年轻人现在就该开始找AI替代不了的工作
查看>>
双十二继续嗨!五位大咖告诉你如何构建自主可控的电商平台
查看>>
“足球走亲”受热捧 福建创新足球启蒙模式
查看>>
英首相提“脱欧”替代方案:成不成,谁说了算?
查看>>
源码视频:HTML5求职必备大牛canvas绘制烟花爆炸特效
查看>>
[GAN学习系列3]采用深度学习和 TensorFlow 实现图片修复(中)
查看>>
Flutter代码模板,解放双手,提高开发效率必备| 掘金技术征文
查看>>
vue自定义指令
查看>>
Flexbox学习笔记-flex项目属性
查看>>
Mac文本编辑技巧
查看>>
异步网络模块之aiohttp的使用
查看>>
技术性能领先,阿里云网络产品全面升级为企业级
查看>>
『技术分享』—— 我的第一个微信小程序-趣闻
查看>>
像仓管一样管理redux-仓管也需要才艺(中间件)
查看>>
基于 HTML5 OpenLayers3 实现 GIS 电信资源管理系统
查看>>