我需要为在其更正操作日期的180天内使用更正操作类型in列表进行更正操作的任何员工拉入不同的员工in。
因此,如果我的员工John Doe在2018年7月8日获得了类型id为123的更正操作,我需要知道他是否在当前更正操作之前的180天或之后的60天内获得了id为123、456或789的更正操作。
在上面的示例表中,我希望它引入员工ID 1和5,因为他们在180天内采取了纠正措施。
发布于 2019-07-09 03:06:56
您可以使用join
来实现这一点
select c.*, cnext.*
from corrective c join
corrective cnext
on cnext.employee_id = c.employee_id and
cnext.ca_dt > c.ca_dt - 180 and
cnext.ca_dt < c.ca_dt + 60 and
cnext.ca_type in (123, 456, 789)
发布于 2019-07-09 01:07:08
Berto,除了Joakim对DDL和样本数据的请求之外,你说的“当前纠正行动”意味着最后一次纠正行动,那么60天后怎么会有纠正行动呢?这里有一些东西可以帮助你表达你的需求:
set echo on
set feedback on
drop table t1;
create table t1 (empid number, corr_action_type number(04), corr_action_date date);
insert into t1 values (7654,123,sysdate - 140);
insert into t1 values (7654,456,sysdate - 092);
insert into t1 values (7654,789,sysdate - 062);
insert into t1 values (7521,123,sysdate - 120);
insert into t1 values (7521,256,sysdate - 120);
select e.*, c.*
from emp e
join (select empid,
min(corr_action_date) oldest,
max(corr_action_date) mostrecent
from t1 where corr_action_type in (123,456,789)
group by empid
) c
on c.empid = e.empno
where
( c.oldest <> c.mostrecent and
c.oldest > c.mostrecent - 180
)
/
https://stackoverflow.com/questions/56939090
复制相似问题