为什么查询显示“无效的关系运算符”

问题描述 投票:0回答:1
select * 
from employees
 where department_id,salary in (
    select department_id,max(salary) 
    from employees group by department_id
 )
sql oracle group-by max greatest-n-per-group
1个回答
1
投票

您想进行元组比较-您需要在in左侧的列元组周围加上括号:

select * 
from employees 
where (department_id,salary) in (
    select department_id, max(salary) from employees group by department_id
)

请注意,可以使用窗口函数更有效地表达此前1组查询:

select *
from (
    select e.*, rank() over(partition by department_id order by salary desc nulls last) rn
    from employees e
) t
where rn = 1
© www.soinside.com 2019 - 2024. All rights reserved.