我有一个带有以下per_all_assignments_m
-列的表>
per_assignments
Person_id position_id system_person_type start_date END_DT 1 1 EMP 01-JAN-2019 20-JAN-2019 1 1 EMP 21-JAN-2019 31-DEC-4712 2 1 EMP 01-JAN-2019 03-JUL-2019 2 1 EMP 04-JUL-2019 08-SEP-2019 2 2 EMP 09-SEP-2019 31-DEC-2019 2 2 EMP 01-JAN-2020 31-DEC-4712 3 10 EMP 01-JAN-2019 20-JAN-2019 3 10 EMP 21-JAN-2019 08-SEP-2019 3 10 EMP 09-SEP-2019 20-JAN-2020 3 10 EMP 21-JAN-2020 31-DEC-4712
如果列position_id的值发生任何变化,我都会创建以下查询以进行提取。该查询将获取日期更改时的日期和上一个开始日期。
select person_id, prev_start_dt, effective_start_date current_start_dt, case pos_new when pos_old then 1 else pos_old end pos_old from ( select person_id, position_id pos_new, effective_start_date, effective_end_date, lag(position_id) over (partition by person_id order by effective_start_date) pos_old, lag(effective_start_date) over (partition by person_id order by effective_start_date) prev_start_dt, case effective_start_date when 1 + lag(effective_end_date) over (partition by person_id order by effective_start_date) then 1 end flag from per_all_assignments_m where person_id=1 and assignment_type = 'E') where flag = 1 and (pos_new <> pos_old )
对于上表,此查询将获取雇员#2的09-SEP-2019作为current_start_dt,获取04-JUL-2019的prev_start_dt
Question
-
现在,我想添加一个条件,如果position_id值没有变化,则应该检索最旧的有效起始日期。
示例-对于EE#1,current_start_dt应该是2019年1月1日,而对于EE#3,prev_start_dt应该是相同的,current_start_dt和prev_start_dt应该是2019年1月1日。
任何建议都会有很大帮助!
必需的输出-
Person_id prev_start_dt current_start_dt pos_old
1 01-JAN-2019 01-JAN-2019 1
2 04-JUL-2019 09-SEP-2019 1
3 01-JAN-2019 01-JAN-2019 10
我有一个带有以下各列的表per_all_assignments_m- per_assignments Person_id position_id system_person_type start_date END_DT 1 1 EMP ...
如果要跟踪所有位置ID发生变化的情况,则应执行以下操作:
这里是一种方法: