Oracle deletes a column of duplicate rows in the table

Source: Internet
Author: User

Oracle deletes a column of duplicate rows in the table

Table Name: T4

Create Table Test. T4
(
ID number (10 ),
Firstname varchar2 (50 char ),
Lastname varchar2 (50 char)
)

The ID is the primary key, and all rows with duplicate IDs are deleted (no matter whether other columns are repeated)

DELETE FROM T4 C
WHERE C.ROWID NOT IN ( SELECT MAX (A.ROWID)
FROM T4 A
GROUP BY A.ID);

Ideas:

1. group all rows and obtain the maximum rowid. The result contains the row that is not repeated and the rowid of the last row that is repeated.

  SELECT MAX (A.ROWID)
FROM T4 A
GROUP BY A.ID;

2. delete other rows in the table.

Test data:

INSERT INTO T4     VALUES (1, 'F1', 'L1');

INSERT INTO T4 VALUES (2, 'F2', 'L2');

INSERT INTO T4 VALUES (3, 'F3', 'L3');

INSERT INTO T4 VALUES (2, 'F2', 'L2');

INSERT INTO T4 VALUES (3, 'F3', 'L3');

1 F1 L1
2 F2 L2
2 F2 L2
3 F3 L3
3 F3 L3

Test results:

1 F1 L1
2 F2 L2
3 F3 L3

Another method:

DELETE FROM tb1      WHERE     tb1.c1 IN (  SELECT tb1.c1                               FROM tb1                           GROUP BY tb1.c1                             HAVING COUNT (tb1.c1) > 1)            AND tb1.ROWID NOT IN (  SELECT MIN (tb1.ROWID)                                      FROM tb1                                  GROUP BY tb1.c1                                    HAVING COUNT (tb1.c1) > 1);

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.