How to Delete the records based upon Prev and Next rows and assign the date based upon certain conditions

后端 未结 2 1857
清歌不尽
清歌不尽 2021-01-16 19:16

This is my insert Statements for the Source data.

REM INSERTING into EXPORT_TABLE  
SET DEFINE OFF;  
Insert into EXPORT_TABLE   values (\'4VKMH\',\'GUIDFREE         


        
2条回答
  •  南方客
    南方客 (楼主)
    2021-01-16 20:08

    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
    

提交回复
热议问题