How does an Oracle database Delete redundant duplicate data ?, Oracle Database

Source: Internet
Author: User

How does an Oracle database Delete redundant duplicate data ?, Oracle Database

This problem is often encountered in Oracle. There is a table a, where multiple data with different IDs but the same information exists. The requirement is to delete redundant duplicate data.

1. Prepare the table test

create table test(id number not null primary key, day date not null); 

2. Prepare data

insert into test(id, day) values(4, to_date('2006-10-08','yyyy-mm-dd'));  insert into test(id, day) values(5, to_date('2006-10-08','yyyy-mm-dd'));  insert into test(id, day) values(6, to_date('2006-10-10','yyyy-mm-dd'));  insert into test(id, day) values(7, to_date('2006-10-08','yyyy-mm-dd'));  insert into test(id, day) values(8, to_date('2006-10-10','yyyy-mm-dd'));  insert into test(id, day) values(9, to_date('2006-10-11','yyyy-mm-dd'));  insert into test(id, day) values(10, to_date('2006-10-11','yyyy-mm-dd'));  insert into test(id, day) values(11, to_date('2006-10-12','yyyy-mm-dd'));  insert into test(id, day) values(12, to_date('2006-10-12','yyyy-mm-dd')); 

Next we will solve this problem through the most common grouping function method.

The core is to delete an Oracle Rowid. (Oracle is indeed powerful and provides the concept of pseudo columns. A pseudo column uniquely identifies a data entry during an insert operation .)

Not much nonsense, dry goods:

 
delete from test awhere a.day in(select day from test group by day having count(*) > 1)and rowid not in(select min(rowid) from test group by day having count(*) > 1)

The preceding is the result SQL code. This code can delete redundant duplicate data.

Core code:
Where a. day in (select day from test group by day having count (*)> 1) grouping the current table to find duplicate items.
and rowid not in(select min(rowid) from test group by day having count(*) > 1)
Duplicate items are found, and the rowid of the pseudo column is used to display the minimum (or maximum) pseudo columns while repeating. You can delete a column with a unique identifier.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.