我试图在下面的案例中找到员工的最低和最高工资以及 dept_id-
Emp
表:
emp_id dept_id salary
----------------------
1 1 100
1 2 200
1 3 300
所需的输出:
emp_id dept_id salary
---------------------
1 1 100
1 3 300
这是我想出来的,但不确定这是否正确-
select emp_id, dept_id, salary
from emp x
where salary in (select min(sal)
from emp y
where y.emp_id = x.emp_id)
or salary in (select max(sal)
from emp y
where y.emp_id = x.emp_id)
答案 0 :(得分:0)
我认为你可以这样做:
select emp_id, dept_id, min(salary) as min_salary, max(salary) as max_salary
from Employees
group by emp_id
此查询应该使用 canplaythrough
event(这是 Oracle 文档,基于您的标记)以更清晰的格式执行您的请求。
考虑详细了解分组集。
答案 1 :(得分:0)
如果您希望行具有最低和最高工资,那么一种方法使用窗口函数:
select emp_id, dept_id, salary
from (select e.*,
row_number() over (partition by emp_id order by salary asc) as seqnm_asc,
row_number() over (partition by emp_id order by salary desc) as seqnm_desc
from emp e
) e
where 1 in (seqnum_asc, seqnum_desc);