如何查找部门中第二高薪资如果部门中只有一个薪资,则显示第一高薪资。输入为
dept_id salary
10 500
10 600
10 1000
20 800
20 900
20 200
30 1200输出
dept_id salary
10 600
20 800
30 1200发布于 2020-04-02 05:47:34
您可以使用窗口函数:
select dept_id, salary
from (
select
t.*,
row_number() over(partition by dept_id order by salary desc) rn,
count(*) over(partition by dept_id) cnt
from mytable t
) t
where rn = 2 or (rn = 1 and cnt = 1)子查询通过降低工资来对具有相同部门的记录进行排名,并计算每个部门有多少名员工。然后,可以使用此信息在外部查询中进行过滤。
https://stackoverflow.com/questions/60980840
复制相似问题