要求:入职在2005年之前(老员工才有资格中奖 ),并且中奖员工是尾数为0的员工,我们要联系所在部门颁发奖品。
select d.department_id, d.department_name
from departments d
where exists (select 1
from employees e
where e.department_id = d.department_id
and e.employee_id in
(110, 120, 130, 140, 150, 160, 170, 180, 190, 200))
and not exists
(select 1
from employees e
where e.department_id = d.department_id
and e.hire_date > to_date('2005-01-01', 'yyyy-mm-dd'))
执行计划
丢东西了,日啊
(select d.department_id,
d.department_name,
max(case
when e.employee_id IN
(110, 120, 130, 140, 150, 160, 170, 180, 190, 200) then
1
else
null
end) as existscontion,
max(case
when e.hire_date > to_date('2005-01-01', 'yyyy-mm-dd') then
1
else
null
end) as notexistscondition,
d.department_id
from departments d
left join employees e
on d.department_id = e.department_id
group by d.department_id, d.department_name ,d.department_id)
结果
select rs.department_id, rs.department_name
from (select d.department_id,
d.department_name,
max(case
when e.employee_id IN
(110, 120, 130, 140, 150, 160, 170, 180, 190, 200) then
1
else
null
end) as existscontion,
max(case
when e.hire_date > to_date('2005-01-01', 'yyyy-mm-dd') then
1
else
null
end) as notexistscondition
from departments d
left join employees e
on d.department_id = e.department_id
group by d.department_id, d.department_name) rs
where existscontion is not null
and notexistscondition is null
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29990276/viewspace-1782010/ ,如需转载,请注明出处,否则将追究法律责任。