How to delete repeated Oracle records

Source: Internet
Author: User

There are often repeated records in Oracle databases. "Oracle Repeated Records" have two meanings: Oracle Repeated Records. One is completely Repeated Records, that is, record where all fields are repeated. Second, record with duplicate key fields. For example, if the Name field is repeated, other fields may not be repeated or can be ignored.

1. For the first type of Oracle repeat record, it is easy to solve. You can use select distinct * from tableName to obtain the result set without repeated record.

If the table needs to delete duplicate records and retain one record), you can delete the record as follows:

Select distinct * into # Tmp
From tablenameddrop table tableNameselect * into tableName
From # Tmpdrop table # Tmp
The reason for this repetition is that the table design is not weekly. You can add a unique index column.

2. This type of Oracle repeat record usually requires that the first record in the repeat record be retained. The procedure is as follows:

Assume that the duplicate fields are Name and Address. You must obtain the unique result set of the two fields.
Select identity (int, 1, 1) as autoID, * into # Tmp
From tableNameselect min (autoID)
As autoID into # Tmp2
From # Tmp group by Name, autoIDselect *
From # Tmp where autoID in (select autoID from # tmp2)
The Name is obtained in the last select statement. The Address does not have duplicate result sets, but an autoID field is added. This column can be omitted in the select clause during actual writing)

Implementation of oracle query current time

Learn more about Oracle hierarchical Query

Provides you with an in-depth understanding of Oracle temporary tables

Oracle with statement usage

Common ORACLE Data Types

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.