【发布时间】:2021-11-15 07:24:44
【问题描述】:
假设所有员工及其部门都列在同一张表employees 中。你如何找到按部门计算的第 n 大薪水?
我知道this 让你非常接近。
select SAL from EMPLOYEE E1 where
(N - 1) = (select count(distinct(SAL))
from EMPLOYEE E2
where E2.SAL > E1.SAL )
但是添加 groupby 仍然不会让你到达那里,除非你在条件下加入了?
select SAL from EMPLOYEE E1
where
(N - 1) = (select count(distinct(SAL))
from EMPLOYEE E2
inner join EMPLOYEE E3 ON department_id
where E2.SAL > E3.SAL )
groupby department
【问题讨论】:
标签: sql sqlite count window-functions correlated-subquery