From: Andreas Kretschmer <andreas@a-kretschmer.de>
To: pgsql-general@lists.postgresql.org
Subject: Re: Deleting duplicate rows using ctid ?
Date: Tue, 6 Feb 2024 18:45:55 +0100
Message-ID: <ac814db0-2882-48fb-84cb-04a9bdc3dc57@a-kretschmer.de> (raw)
In-Reply-To: <CAKFQuwYKLoEk==403z3FNdh3WJj8Wp_R2O+jN_9h7L3Uqi_Bzg@mail.gmail.com>
References: <CAEs=6DnRvQoV_pCLOU91J9y=xQRKfiJFTdqpk+a4LJay0VeEwg@mail.gmail.com>
<CAKFQuwYKLoEk==403z3FNdh3WJj8Wp_R2O+jN_9h7L3Uqi_Bzg@mail.gmail.com>
Am 06.02.24 um 00:32 schrieb David G. Johnston:
> On Mon, Feb 5, 2024 at 4:09 PM David Gauthier <dfgpostgres@gmail.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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: andreas@a-kretschmer.de
Subject: Re: Deleting duplicate rows using ctid ?
In-Reply-To: <ac814db0-2882-48fb-84cb-04a9bdc3dc57@a-kretschmer.de>
References: <CAEs=6DnRvQoV_pCLOU91J9y=xQRKfiJFTdqpk+a4LJay0VeEwg@mail.gmail.com> <CAKFQuwYKLoEk==403z3FNdh3WJj8Wp_R2O+jN_9h7L3Uqi_Bzg@mail.gmail.com> <ac814db0-2882-48fb-84cb-04a9bdc3dc57@a-kretschmer.de>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox