获取“转移”之类特定操作的优先级

时间:2015-07-06 07:06:27

标签: sql oracle oracle-fusion-apps

我需要获取员工的转出行,这意味着先前的行动或行动'转移'的前一行。目前我只通过effective_start_date条件来实现它。目前这是我的代码: -

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)

但是通过这段代码,我找不到那些在他们的工作记录中只有两行的员工 - 假设一个是雇用而另一个是转移。对于那些员工的“雇佣”行动将是转出行。但两者都在同一天。只有有效的顺序是不同的。对于这些员工,我需要添加有效的序列逻辑以及effective_start_date。我写了代码: -

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'

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

1 个答案:

答案 0 :(得分: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 demo

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