2016-09-19 99 views
0

我有一个bitemporal表,我需要在每个月保留当前活动行和一个历史记录行。如何从BiTemporal表中删除(物理删除)行

如果行在月内重复,我需要删除这些行。

Policy_ID Customer_ID Validity 

497201 304779902 ('05/06/16', HIGH END) 

    540944 304779902 ('07/25/16', '07/30/16') 

    541077 304779902 ('07/10/16', '07/24/16') 

    541145 304779902 ('07/01/16', '07/10/16') 

    541008 304779902 ('06/20/16', '07/01/16') 

从上面排我需要保持 Policy_ID CUSTOMER_ID有效性


497201 304779902 ('05/06/16', HIGH END) 

    540944 304779902 ('07/25/16', '07/30/16') 

    541008 304779902 ('06/20/16', '07/01/16') 

任何特定的命令,它可以帮助做到这一点。

回答

0

要实际删除行,您需要NONTEMPORAL删除。最简单的方法使用这样的临时表:

create volatile table src as 
(select Policy_ID, Customer_ID, Validity 
    from tab 
    qualify 
    row_number() -- find the rows to delete 
    over (partition by Customer_ID order by Validity desc) > 2 
) with data 
primary index (Customer_ID) -- should be the PI of the target table 
on commit preserve rows; 

nontemporal 
delete from tab 
where (Policy_ID, Customer_ID, Validity) 
in (select * from src)