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 1so2n2-007s2h-Hf for pgsql-general@arkaria.postgresql.org; Tue, 10 Sep 2024 15:28:26 +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 1so2n2-00EtmH-4g for pgsql-general@arkaria.postgresql.org; Tue, 10 Sep 2024 15:28:24 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1so2n1-00EtkT-Hw for pgsql-general@lists.postgresql.org; Tue, 10 Sep 2024 15:28:23 +0000 Received: from ssl.schlittermann.de ([46.38.236.101]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1so2mx-000U6C-7f for pgsql-general@lists.postgresql.org; Tue, 10 Sep 2024 15:28:22 +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=ebt7hXV8+H/HPeT/rROQd1EOcBuQAe4v0lZID9SyUEc=; b=ko90JZGaBm4lvTPS7dpDkaNhTR GtFMFlWjHiGXxJAF1kQjc0vyoeNn2Erhgx0tTVYzQGzbwZlNhQBE5WAZ2BX8GroBnMHHvrIoT5ppv fOpUlYkRFra3NZ5PoFioGu4J9CFD6s+dDsrBMwtF3F+Q3FB9dLUXUHOHoVFzC7mUjwXY=; Received: by ssl.schlittermann.de with esmtpsa (TLS1.3:TLS_AES_128_GCM_SHA256:128) (Exim 4.x) (envelope-from ) id 1so2mt-0000000H1i6-49Zl for pgsql-general@lists.postgresql.org; Tue, 10 Sep 2024 17:28:16 +0200 Message-ID: <642a6ec8-4ec3-4f2a-b39a-6855237a34c1@a-kretschmer.de> Date: Tue, 10 Sep 2024 17:28:15 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Removing duplicate rows in table Content-Language: en-US To: pgsql-general@lists.postgresql.org References: 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 10.09.24 um 17:07 schrieb Rich Shepard: > I've no idea how I entered multiple, identical rows in a table but I > want to > delete all but one of these rows. > > Here's an example: > > bustrac=# select * from projects where proj_nbr = '4242.01'; >  proj_nbr |   proj_name    | start_date |  end_date  | description  | > notes > ----------+----------------+------------+------------+---------------+------- >  4242.01  | Expert witness | 2008-10-15 | 2008-10-28 | Consol Energy | >  4242.01  | Expert witness | 2008-10-15 | 2008-10-28 | Consol Energy | >  4242.01  | Expert witness | 2008-10-15 | 2008-10-28 | Consol Energy | >  4242.01  | Expert witness | 2008-10-15 | 2008-10-28 | Consol Energy | > (4 rows) > > How do I clean this up so there's only a single row for this project > number? > > TIA, > > Rich > > you can use the hidden ctid-column: postgres=# create table demo (id int, val text); CREATE TABLE postgres=# insert into demo values (1, 'test1'); INSERT 0 1 postgres=# insert into demo values (1, 'test1'); INSERT 0 1 postgres=# insert into demo values (1, 'test1'); INSERT 0 1 postgres=# insert into demo values (1, 'test1'); INSERT 0 1 postgres=# select ctid, * from demo;  ctid  | id |  val -------+----+-------  (0,1) |  1 | test1  (0,2) |  1 | test1  (0,3) |  1 | test1  (0,4) |  1 | test1 (4 rows) postgres=# with my_ctid as (select min(ctid) from demo where id = 1 and val = 'test1') delete from demo using my_ctid where id=1 and val='test1' and ctid != my_ctid.min; DELETE 3 postgres=# select ctid, * from demo;  ctid  | id |  val -------+----+-------  (0,1) |  1 | test1 (1 row) postgres=# -- Andreas Kretschmer CYBERTEC PostgreSQL Services and Support