You don't need group by clause
On from clause
, you can use 2 auto join
to find next and previous rows using index.
On the select clause
you need just adapt case when
statement to implement your specific rules like this (not tested, but the idea is here) :
create table RHS as
select
Curr.policy,
case
when prev.EventDate is null then Curr.policyIssueDate
else EventDate
end as StartDate,
case
when next.EventDate is null then CURDATE()
else EventDate
end as EndDate
from
LHS Curr
left join LHS next on
Curr.INDEX+1= next.INDEX and
Curr.policy = next.policy
left join LHS prev on
Curr.INDEX-1= prev.INDEX and
Curr.policy = prev.policy
For today date :
Note : you can also use lead
and lag
window function.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments