+------------+----------+--------+ | Department | Employee | Salary | +------------+----------+--------+ | IT | Max | 90000 | | IT | Randy | 85000 | | IT | Joe | 85000 | | IT | Will | 70000 | | Sales | Henry | 80000 | | Sales | Sam | 60000 | +------------+----------+--------+
解释:
IT 部门中,Max 获得了最高的工资,Randy 和 Joe 都拿到了第二高的工资,Will 的工资排第三。销售部门(Sales)只有两名员工,Henry 的工资最高,Sam 的工资排第二。
解题思路
Employee join Employee,取出比自己大的数目小于3的
再取所需维度,where id 在上面筛选出来的
hive中直接用 row_number() over(partition by DepartmentId order by Salary desc) as rank,外面再包一层 rank <= 3 即可
code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
# Write your MySQL query statement below select d.Name as'Department', c.Name as'Employee', c.Salary as'Salary' from Employee c inner join Department d on c.DepartmentId = d.Id where c.Id in (select a.Id from Employee a left join Employee b on a.DepartmentId = b.DepartmentId and a.Salary < b.Salary group by a.Id having count(distinct b.Salary) < 3) order by d.Name, c.Salary desc, c.Name