2015-07-06 72 views
-1

我需要走出獲取轉移員工的行,這意味着之前的動作或動作「轉移」的前行中的具體行動。目前我正在通過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) 

但是這段代碼我沒有得到誰只有兩個排在他們的工作record-假設一個這些員工是出租,另一個轉移。對於那些僱員'僱用'行動將是轉出行。但兩者都是在同一天。只有有效的順序是不同的。對於這些員工,我需要添加有效的順序邏輯以及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' 

但是這也沒有給我相同的有效日期的行動爲轉移的員工。請幫助我。

回答

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和主查詢(最後兩行)最後「轉移」之前行到每一傳輸分配。

相關問題