我有一个双时态表,我需要在每个月保持当前活动行和一个历史记录行。
如果行在月内重复,我需要删除这些行。
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 :(得分: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)