Re: Delete Duplicates with Using

From: Andreas Kretschmer <andreas(at)a-kretschmer(dot)de>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Delete Duplicates with Using
Date: 2017-10-16 06:53:38
Message-ID: 74bce063-5902-044b-7e3d-634ce17df891@a-kretschmer.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Am 14.10.2017 um 08:20 schrieb Igal @ Lucee.org:
>
> Hello,
>
> I run the SQL query below to delete duplicates from a table. The
> subquery is used to identify the duplicated rows (row_num is a
> BIGSERIAL column).
>

other solution, using the CTID-column: (rows with (1,1,1) and (5,5,5)
are identical)

test=*# select * from dubletten ;
c1 | c2 | c3
----+----+----
1 | 1 | 1
1 | 1 | 1
1 | 2 | 3
2 | 3 | 4
3 | 4 | 5
4 | 5 | 5
5 | 5 | 5
5 | 5 | 5
(8 Zeilen)

test=*# with keep as (select max(ctid) as ctid from dubletten group by
c1,c2,c3) delete from dubletten where ctid not in (select ctid from keep);;
DELETE 2
test=*# select * from dubletten ;
c1 | c2 | c3
----+----+----
1 | 1 | 1
1 | 2 | 3
2 | 3 | 4
3 | 4 | 5
4 | 5 | 5
5 | 5 | 5
(6 Zeilen)

test=*#

Regards, Andreas

--
2ndQuadrant - The PostgreSQL Support Company.
www.2ndQuadrant.com

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Thomas Kellerer 2017-10-16 07:19:54 Postgres 10 manual breaks links with anchors
Previous Message Igal @ Lucee.org 2017-10-16 03:10:22 Re: Adding identity column to a non-empty table