Mysql每日一题(分组排序问题dense_rank,困难)
这里是一个典型的分组排序问题,如果使用dense_rank() over(partition by departmentId order by salary desc)的方法可能不是很难,就是需要注意代码不要写错了dense_rank()这段代码一定要写在select内
代码如下:
select se.name Department,fi.name Employee,fi.salary Salary
from (
select *,
dense_rank() over(partition by departmentId order by salary desc) ranking
from Employee
) as fi left join Department se on fi.departmentId=se.id
where ranking<=3;
这段代码应该还是很清晰的,主要问题就是写代码对窗口函数dense_rank使用的问题
但如果不用窗口函数就难了
我给出官方代码,大家看看就行了,我也看了很久才稍微看懂
SELECT
d.Name AS 'Department', e1.Name AS 'Employee', e1.Salary
FROM
Employee e1
JOIN
Department d ON e1.DepartmentId = d.Id
WHERE
3 > (SELECT
COUNT(DISTINCT e2.Salary)
FROM
Employee e2
WHERE
e2.Salary > e1.Salary
AND e1.DepartmentId = e2.DepartmentId
)
;
这是别人给的解释,我也是结合别人的解释才看懂的
原文地址:https://blog.csdn.net/2401_87603544/article/details/143701230
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!