PostgreSQL SQL statement for deleting duplicate data

Source: Internet
Author: User
Tags postgresql

How does the PostgreSQL database remove duplicate data from a single table? You can use ctid. The following is the experiment process.


I. Create a test table

The code is as follows: Copy code
David = # create table emp (
David (# id int,
David (# name varchar );
CREATE TABLE
David = #

2. Insert test data

The code is as follows: Copy code


David = # insert into emp values (1, 'David ');
INSERT 0 1
David = # insert into emp values (1, 'David ');
INSERT 0 1
David = # insert into emp values (1, 'David ');
INSERT 0 1
David = # insert into emp values (2, 'Sandy ');
INSERT 0 1
David = # insert into emp values (2, 'Sandy ');
INSERT 0 1
David = # insert into emp values (3, 'Renee ');
INSERT 0 1
David = # insert into emp values (4, 'Jack ');
INSERT 0 1
David = # insert into emp values (5, 'rose ');
INSERT 0 1
David = #

III. Query initialization data

The code is as follows: Copy code


David = # select ctid, * from emp;
Ctid | id | name
------- + ---- + -------
(0, 1) | 1 | david
(0, 2) | 1 | david
(0, 3) | 1 | david
(0, 4) | 2 | sandy
(0, 5) | 2 | sandy
(0, 6) | 3 | renee
(0, 7) | 4 | jack
(0, 8) | 5 | rose
(8 rows)

David = #

Query repeated data count

The code is as follows: Copy code


David = # select distinct id, count (*) from emp group by id having count (*)> 1;
Id | count
---- + -------
1 | 3
2 | 2
(2 rows)

David = #

Three records with id 1 and two records with id 2 are found.

4. Query the data to be retained

Take min (ctid) or max (ctid) as the standard.

The code is as follows: Copy code


David = # select ctid, * from emp where ctid in (select min (ctid) from emp group by id );
Ctid | id | name
------- + ---- + -------
(0, 1) | 1 | david
(0, 4) | 2 | sandy
(0, 6) | 3 | renee
(0, 7) | 4 | jack
(0, 8) | 5 | rose
(5 rows)

David = #

5. Delete duplicate data

The code is as follows: Copy code

David = # delete from emp where ctid not in (select min (ctid) from emp group by id );
DELETE 3
David = #


6. View the final result

The code is as follows: Copy code


David = # select ctid, * from emp;
Ctid | id | name
------- + ---- + -------
(0, 1) | 1 | david
(0, 4) | 2 | sandy
(0, 6) | 3 | renee
(0, 7) | 4 | jack
(0, 8) | 5 | rose
(5 rows)

David = # Note: If the table already has a unique sequence primary key value, you can replace the value with ctid and delete it directly.

Related Article

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.