从每个分区中查找 sql 中的 mix 和 max
Find mix and max in sql from each partition
我正在尝试查找员工的最低和最高工资以及以下情况中的 dept_id-
Emp
table:
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)
我想你可以这样做:
select emp_id, dept_id, min(salary) as min_salary, max(salary) as max_salary
from Employees
group by emp_id
此查询应使用 the "group by" clause(这是 Oracle 文档,基于您的标记)以更简洁的格式执行您的请求。
考虑详细了解分组集。
如果您想要 行 具有最低和最高工资,那么一种方法使用 window 函数:
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);
我正在尝试查找员工的最低和最高工资以及以下情况中的 dept_id-
Emp
table:
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)
我想你可以这样做:
select emp_id, dept_id, min(salary) as min_salary, max(salary) as max_salary
from Employees
group by emp_id
此查询应使用 the "group by" clause(这是 Oracle 文档,基于您的标记)以更简洁的格式执行您的请求。
考虑详细了解分组集。
如果您想要 行 具有最低和最高工资,那么一种方法使用 window 函数:
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);