Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rXPWT-00AVYq-MN for pgsql-general@arkaria.postgresql.org; Tue, 06 Feb 2024 17:46:18 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1rXPWS-005bIe-FC for pgsql-general@arkaria.postgresql.org; Tue, 06 Feb 2024 17:46:16 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rXPWR-005bIV-QQ for pgsql-general@lists.postgresql.org; Tue, 06 Feb 2024 17:46:16 +0000 Received: from ssl.schlittermann.de ([46.38.236.101]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rXPWK-005nqj-6B for pgsql-general@lists.postgresql.org; Tue, 06 Feb 2024 17:46:14 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=schlittermann.de; s=dd201504; h=Content-Transfer-Encoding:Content-Type: In-Reply-To:From:References:To:Subject:MIME-Version:Date:Message-ID:Cc: Content-Description:Content-ID:Resent-Cc:Resent-To; bh=si0ayRP6nd4mUAsRUoB3vugL2w2xquL1dX6vBTELVto=; b=sjz9lx/YsfC/uILmVQqprnCW1C CKHTIdQ/1VXeOGzgeEji5B96LNAUE2TmWCxHwAWrpFr3QqkgFq5lb1Wn0AP6wdH2KpXnu7t4tEuHe YbG5vsjb0Mu7P8N6ARtbLZHIWiNoxVD8OOAGYgdSoTLfee+ZzzKfMw16UPlN7vAgL3VY=; Received: by ssl.schlittermann.de with esmtpsa (TLS1.3:TLS_AES_128_GCM_SHA256:128) (Exim 4.x) (envelope-from ) id 1rXPWJ-006bTk-2v for pgsql-general@lists.postgresql.org; Tue, 06 Feb 2024 18:46:07 +0100 Message-ID: Date: Tue, 6 Feb 2024 18:45:55 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Deleting duplicate rows using ctid ? To: pgsql-general@lists.postgresql.org References: Content-Language: en-US From: Andreas Kretschmer In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Am 06.02.24 um 00:32 schrieb David G. Johnston: > On Mon, Feb 5, 2024 at 4:09 PM David Gauthier > 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