Re: Deleting duplicate rows using ctid ?

From: Andreas Kretschmer <andreas(at)a-kretschmer(dot)de>
To: pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Re: Deleting duplicate rows using ctid ?
Date: 2024-02-06 17:45:55
Message-ID: ac814db0-2882-48fb-84cb-04a9bdc3dc57@a-kretschmer.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Am 06.02.24 um 00:32 schrieb David G. Johnston:
> On Mon, Feb 5, 2024 at 4:09 PM David Gauthier <dfgpostgres(at)gmail(dot)com>
> wrote:
>
>
> I want the result to be just 2 recs, one for each dog.
>
>
> My present goto link for this question:
>
> https://harshitjain.home.blog/2019/06/17/postgresql-how-to-delete-duplicate-rows-in-postgresql/
>
> David J.
>

postgres=# select * from dogs;
 dog
------
 dog1
 dog1
 dog2
 dog2
 dog2
 dog3
(6 rows)

postgres=# select ctid, dog, row_number() over (partition by dog) from
dogs ;
 ctid  | dog  | row_number
-------+------+------------
 (0,1) | dog1 |          1
 (0,2) | dog1 |          2
 (0,3) | dog2 |          1
 (0,4) | dog2 |          2
 (0,5) | dog2 |          3
 (0,6) | dog3 |          1
(6 rows)

postgres=# with ct as (select ctid, dog, row_number() over (partition by
dog) from dogs) delete from dogs where ctid in (select ctid from ct
where row_number != 1) ;
DELETE 3
postgres=# select * from dogs;
 dog
------
 dog1
 dog2
 dog3
(3 rows)

postgres=#

Regards, Andreas

--
Andreas Kretschmer
CYBERTEC PostgreSQL Services and Support

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Ron Johnson 2024-02-06 18:11:23 Exclude certain application pgaudit logging?
Previous Message Laurenz Albe 2024-02-06 10:16:04 Re: Question on partitioning