Here it is..
sel a.empid as a1, min(b.effdate) as leave_start_date, a.effdate as leave_end_date from
(sel empid,effdate,
coalesce(
min(effdate)over(partition by empid order by effdate rows between 1 preceding and 1 preceding)
,cast('1900-01-01'as date)) as n1
from mkdate where emp_stat='ACTIVE'
)a
left join
(sel empid,effdate from mkdate where emp_stat='LEAVE')b
on a.empid = b.empid and a.effdate >= b.effdate and a.n1 < b.effdate
group by 1,3
order by 1,3,2;
Regards,
Mohan K
↧