sql-Delete duplicate records

Source: Internet
Author: User
Tags db2

The first few days of work, there is a small need to delete a table table_a duplicate records (keep one), assuming that the field col_pk repetition to determine the record duplication, then there are several ways:

In Oracle, you can use ROWID to delete, which is a very efficient way of writing:

DELETE from Table_a WHERE ROWID isn't in (SELECT MIN (ROWID), col_pk from Table_a GROUP by COL_PK); --First, according to COL_PK Group, find the smallest rowid, then all non-minimum values are repeated records, delete it.

Unfortunately, my development environment is the DB2 database, DB2 database is not so convenient to use ROWID, but DB2 data can take advantage of the row_number () function, which is not used by Oracle:

DELETE from (SELECT row_number ()-Over (PARTITION by COL_PK ORDER by COL_PK) as-NO from table_a) WHERE no>1;

Further development of the above ideas:

How do I delete all duplicate records (do not keep one)?

Oracle:delete from Table_a WHERE ROWID not in (SELECT MIN (ROWID) from Table_a GROUP by PK have COUNT (1) =1)

Db2&oracle:delete from Table_a WHERE PK in (SELECT PK from table_a GROUP by PK have COUNT (1) >1)

sql-Delete duplicate records

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.