mysql - 如何获得比当前元组有更好薪水的人数
问题描述
我正在解决 Leetcode #185中的一个问题
我可以理解该解决方案,但我想知道如何编写查询以添加一列,该列指示人们的薪水比元组高的人数。我认为这在 SQL 中是可能的,但我不知道如何使它正确,我总是得到语法错误。:-/
from Employee e1 (Select count(distinct e2.Salary)
from Employee e2
Where e2.Salary > e1.Salary) as c
例如,我有这样一个表 Employee:
Id - Name - Salary
1 toto 60000
2 tata 50000
3 kiki 90000
4 lily 70000
5 momo 60000
我想要这样的结果:
Id - Name - Salary - Head_count_of_higher_salary
1 toto 60000 2
2 tata 50000 4
3 kiki 90000 0
4 lily 70000 1
5 momo 60000 2
多谢你们
解决方案
If you are running MySQL 8.0: what you ask for is exactly what rank()
does.
This would be as simple as:
select e.*, rank() over(order by salary desc) - 1 head_count_of_higher_salary
from employees e
推荐阅读
- java - 从 Maven 命令行 TestNG 更改 .properties 值
- orbeon - Orbeon 表单 - 动态填充复选框
- entity-framework-6 - ASP.Net Web API 2 中的无参数构造函数错误
- android - 根据图像的非特定纵横比的图像的纵横比调整ImageView的大小
- vue.js - Vue 组件:在插槽中使用插槽
- python - 如何将具有列表组件的numpy数组放入for循环中
- python - 如何处理 tf.keras 中输出标签的“无效参数错误”
- android - 在android中的适配器内进行改造调用
- amazon-web-services - AWS Codeguru 审查建议不起作用
- kubernetes - 通过 vscode 的状态栏启动 minikube 出错