This is my insert Statements for the Source data.
REM INSERTING into EXPORT_TABLE
SET DEFINE OFF;
Insert into EXPORT_TABLE values (\'4VKMH\',\'GUIDFREE
Let me state up front that I didn't test this in Oracle as I don't have an Oracle database handy.
I pared it down to a single join, it may be useful to compare performance with the accepted answer.
select
e1.reg_id,
e1.product_cd,
e1.event_type,
e1.event_date,
e1.term_start_date,
case e1.event_type when 'CANCELLATION' then e1.term_end_date else coalesce(e2.term_end_date, e1.term_end_date) end as term_end_date,
e1.days,
e1.amt
from event e1
left outer join event e2 on
e1.reg_id = e2.reg_id and
e1.product_cd = e2.product_cd and
e1.term_start_date = e2.term_start_date and
(e1.event_type = 'CANCELLATION' or e2.event_type = 'CANCELLATION') and
e1.event_date <> e2.event_date
where trunc(e1.event_date) <> trunc(e2.event_date) or e2.reg_id is null