Deleting Duplicate Records in Oracle based on Maximum Date/Time

a 夏天 提交于 2019-12-03 17:27:01

Something like:

DELETE FROM the_table_with_no_name 
WHERE date_column != (SELECT MAX(t2.date_column) 
                      FROM the_table_with_no_name t2 
                      WHERE t2.id = the_table_with_no_name.id);

You can generate the ROWIDs of all rows other than the one with the maximum date (for a given EMPIds) and delete them. I have found this to be performant as it is a set-based approach and uses analytics, rowIDs.

--get list of all the rows to be deleted.

select row_id from (
select rowid row_id,
       row_number() over (partition by emp_id order by date desc) rn
  from <table_name>
) where rn <> 1

And then delete the rows.

delete from table_name where rowid in (
    select row_id from (
    select rowid row_id,
           row_number() over (partition by emp_id order by date desc) rn
      from <table_name>
    ) where rn <> 1
);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!