-1

我需要获取员工的转出行,这意味着先前的操作或操作“转移”的前一行。目前我仅通过有效开始日期条件来完成它。目前这是我放的代码:-

SELECT PAAM.EFFECTIVE_START_DATE, PAAM.PERSON_ID,PAAM.ACTION_CODE FROM PER_ALL_ASSIGNMENTS_M PAAM 
WHERE
TO_CHAR(PAAM.EFFECTIVE_START_DATE,'YYYY-MM-DD') = 
(SELECT TO_CHAR(MAX(PAAM1.EFFECTIVE_START_DATE),'YYYY-MM-DD') FROM FUSION.PER_ALL_ASSIGNMENTS_M PAAM1 
WHERE PAAM1.EFFECTIVE_START_DATE< (SELECT MAX(EFFECTIVE_START_DATE) FROM FUSION.PER_ALL_ASSIGNMENTS_M PAAM2 WHERE PAAM2.ACTION_CODE='TRANSFER' 
AND PAAM2.PERSON_ID=PAAM1.PERSON_ID AND SYSDATE BETWEEN PAAM2.EFFECTIVE_START_DATE AND PAAM2.EFFECTIVE_END_DATE) AND PAAM1.PERSON_ID=PAAM.PERSON_ID)

但是通过这段代码,我没有得到那些工作记录中只有两行的员工——假设一个是 Hire,另一个是 Transfer。对于这些员工,“雇用”操作将是转出行。但两者是同一天。只有有效顺序不同。对于这些员工,我需要添加有效的序列逻辑以及有效的开始日期。我已经写了代码:-

select  PAAM1.effective_start_date, paam1.effective_end_date, paam1.action_code, paam1.effective_sequence  from PER_ALL_assignments_M PAAM,PER_ALL_ASSIGNMENTS_M PAAM1, per_all_people_f papf
where PAAM.person_id = paam1.person_id
and paam.person_id=papf.person_id 
and paam.effective_start_date=(select max(paam3.effective_start_date) 
from per_all_assignments_m paam3 where paam.person_id=paam3.person_id) 
and paam.effective_sequence=    
(select max (effective_sequence) from per_all_assignments_m paam4 where paam.person_id=paam4.person_id) 
and paam1.effective_start_date = (SELECT MAX(paam5.EFFective_start_DaTe) from per_all_assignments_m paam5 
where paam5.person_id = paam1.person_id and (paam5.effective_start_date < paam.effective_start_date 
or (paam5.effective_start_date = paam.effective_start_date and paam1.effective_sequence < paam.effective_sequence ))) 
and paam1.effective_sequence =(SELECT MAX(paam6.effective_sequence) from per_all_assignments_m paam6 
where paam6.person_id = paam1.person_id and paam6.effective_start_date = paam1.effective_start_date
and (paam1.effective_start_date<paam.effective_start_date or(paam1.effective_start_date=paam.effective_start_date and paam6.effective_sequence< paam.effective_sequence)))
and paam.action_code='TRANSFER'

但这也没有给我与转移相同的有效日期行动的员工。请帮帮我。

4

1 回答 1

0

您的查询和解释很难理解,如果您编辑问题并添加数据示例和预期输出会很好。无论如何,这里是根据指定规则工作的查询(或者至少我是如何理解它们的):

with 
  data as (
    select effective_start_date esd, effective_end_date eed,
           person_id id, action_code action,
           row_number() over (partition by person_id 
                              order by effective_start_date desc, 
                                       effective_sequence desc) rn
      from per_all_assignments_m p),
  transfers as (
    select id, min(rn) rn from data 
      where action = 'TRANSFER' and sysdate between esd and eed group by id )
select d.id, d.esd, d.action 
  from data d join transfers t on d.id = t.id and d.rn = t.rn + 1

SQLFiddle 演示

主要部分是row_number按正确顺序为每个 id 分别枚举行的函数,下一个子查询transfers为每个 id 找到最后一个“TRANSFER”,主查询(最后两行)将前一行分配给每个传输。

于 2015-07-06T14:49:53.990 回答