可以使用lead
功能提前尋找到下一行,並得到其有效日期:
select person_id, effective_date,
lead(effective_date)
over (partition by person_id order by effective_date) as lead_date
from t42;
PERSON_ID EFFECTIVE_DATE LEAD_DATE
---------- -------------- ---------
6335 26-JUN-98 09-JUL-98
6335 09-JUL-98 24-FEB-99
6335 24-FEB-99
然後,您可以用它來執行更新。該merge
命令使這很簡單:
merge into t42
using (
select person_id, effective_date,
lead(effective_date)
over (partition by person_id order by effective_date) as lead_date
from t42
) t
on (t42.person_id = t.person_id and t42.effective_date = t.effective_date)
when matched then
update set t42.end_effective_date =
case
when t.lead_date is null then date '9999-12-31'
else t.lead_date - 1
end;
3 rows merged.
select * from t42;
PERSON_ID EFFECTIVE_DATE END_EFFECTIVE_DATE
---------- -------------- ------------------
6335 26-JUN-98 08-JUL-98
6335 09-JUL-98 23-FEB-99
6335 24-FEB-99 31-DEC-99
的using
條款從上面從上一行得到最新片段。 on
子句將其與原始表匹配,並且匹配的行將結束生效日期更新爲領導生效日期前一天,或者如果沒有前導值(對於最近的「當前」行)使用固定從1999年
你的問題日期提到了一個更新,但如果你只是想結束日期爲您的結果計算列設置它的簡單得多:
select person_id, effective_date,
case when lead_date is null then date '9999-12-31'
else lead_date - 1 end as end_effective_date
from (
select person_id, effective_date,
lead(effective_date)
over (partition by person_id order by effective_date) as lead_date
from t42
);
PERSON_ID EFFECTIVE_DATE END_EFFECTIVE_DATE
---------- -------------- ------------------
6335 26-JUN-98 08-JUL-98
6335 09-JUL-98 23-FEB-99
6335 24-FEB-99 31-DEC-99
什麼是烏拉圭回合的最終efeectiveDate邏輯是什麼? –
你可以請我們你在這裏做什麼? – heretolearn
嗨,我只是編輯我的問題,請看看第一。對不起,沒有把問題弄清楚。 – goh6319