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 1sob64-00CQAz-Lx for pgsql-general@arkaria.postgresql.org; Thu, 12 Sep 2024 04:06:21 +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 1sob63-00EXXK-J5 for pgsql-general@arkaria.postgresql.org; Thu, 12 Sep 2024 04:06:19 +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 1sob63-00EXXB-38 for pgsql-general@lists.postgresql.org; Thu, 12 Sep 2024 04:06:19 +0000 Received: from mail-pj1-x1029.google.com ([2607:f8b0:4864:20::1029]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sob5y-000ke5-Dd for pgsql-general@postgresql.org; Thu, 12 Sep 2024 04:06:18 +0000 Received: by mail-pj1-x1029.google.com with SMTP id 98e67ed59e1d1-2d8a744aa9bso331292a91.3 for ; Wed, 11 Sep 2024 21:06:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bitnine-net.20230601.gappssmtp.com; s=20230601; t=1726113972; x=1726718772; darn=postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=uSGJJfxHv8vxMXY8pU+uIAPixnOsIWh2gG1QizWOfS0=; b=QXW1ikGFCsBiK8BtOw888dxpGHO4QaZBaDI8nmcCLJpcKFvZvBVKqqiqa3mcOr12MG mDMLgizBgj7lVifZWPi4GsqPCfstHI85Utw683+XY3eOtXVOwUkRXb1TZRoDw+jHrPrP LsXy0lPAaNa7n20h7+vFJzZ8Q2cvi1PR/hNxq2iPUzmPBte6f2oSCKIUY52g7XoIVEDi rOfHtleW0lFUw0qGs9o1L1AcN/JsrNHznvnKoVTjgAw0jZdMOqcrxrfqQeudwUvW/zZI rdM7wQ5tozj7H1lPHjDJStDFfA88hLrRT6DIgjyioU3AH+4f/As3F5EVBx914QA7DLXF vpOA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726113972; x=1726718772; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=uSGJJfxHv8vxMXY8pU+uIAPixnOsIWh2gG1QizWOfS0=; b=TlZlf2QtnB419Rpkq1hyzvONBdllhol5nF4b537TVi3eDEBLvLP5/s8v312HIfV1ax mygxnXQouf4oYdpc8mgbs3EsvLBxSOfa8+fPMMImT15vWfIsuLnGTlA5dBOFnO6cWVbu CxB98sUBet8HvpJm4l0TotvatiIyO5W1b5TjvZXJ6s/5nCh7lpsty8XmgtThyi+U/BfX OksDaEZv9ktUEkCwmk1QhF4agj6XlNX+PT/jH/73CUGBR9XnHDKW0lwoLmvUiN+Zu28i EbNKbY6m/kHRbk9Vb913Tf15pWM6SAUik/9IowW+VjQ6LggrGFDgjifywibvi0sHZrPs xnIQ== X-Gm-Message-State: AOJu0YxL3AySb0Hv6eZgifrdBY/Cvw4NRU2wuBxZ8pzLvHteCSIyUKZq 6EKFFOPzQNx9h4ERbQkts38hN3QYaLTO56r4nRmsUINeiLlUBh+gIOdGO/Ooxm8syNPbEmOzyKA V888WTjQQMbKU1ptFbZM8ZfFpG4pxZEvMV5Wl/w== X-Google-Smtp-Source: AGHT+IGyhLqayc+RgXn8/Nj8Oixi3m3NBqJDtAwAV/yWOt7EvCVi2oh5S5oUkJEwgztep11NA2yY57G2nfilB2NlgY4= X-Received: by 2002:a17:90b:3c2:b0:2cf:eaec:d74c with SMTP id 98e67ed59e1d1-2db9ffc1c01mr1555980a91.16.1726113971880; Wed, 11 Sep 2024 21:06:11 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Muhammad Usman Khan Date: Thu, 12 Sep 2024 09:06:00 +0500 Message-ID: Subject: Re: Removing duplicate rows in table To: Rich Shepard Cc: pgsql-general@postgresql.org Content-Type: multipart/alternative; boundary="00000000000022df220621e43ad2" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --00000000000022df220621e43ad2 Content-Type: text/plain; charset="UTF-8" Hi, You can try the following CTE which removes all the identical rows and only leave single row WITH CTE AS ( SELECT ctid, ROW_NUMBER() OVER (PARTITION BY proj_nbr, proj_name, start_date, end_date, description, notes ORDER BY proj_nbr) AS rn FROM projects WHERE proj_nbr = '4242.02' ) DELETE FROM projects WHERE ctid IN ( SELECT ctid FROM CTE WHERE rn > 1 ); On Tue, 10 Sept 2024 at 20:07, Rich Shepard wrote: > 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 > > > --00000000000022df220621e43ad2 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hi,
You can try the follo= wing CTE which removes all the identical rows and only leave single row
=
WITH CTE AS (
=C2=A0 SELECT ctid, ROW_NUMBER() OVER (PARTITION BY pr= oj_nbr, proj_name, start_date, end_date, description, notes ORDER BY proj_n= br) AS rn
=C2=A0 FROM projects
=C2=A0 WHERE proj_nbr =3D '4242.02= '
)
DELETE FROM projects
WHERE ctid IN (
=C2=A0 SELECT ctid= FROM CTE WHERE rn > 1
);


On Tue, 10 S= ept 2024 at 20:07, Rich Shepard <rshepard@appl-ecosys.com> wrote:
I've no idea how I entered multiple, ident= ical rows in a table but I want to
delete all but one of these rows.

Here's an example:

bustrac=3D# select * from projects where proj_nbr =3D '4242.01'; =C2=A0 proj_nbr |=C2=A0 =C2=A0proj_name=C2=A0 =C2=A0 | start_date |=C2=A0 e= nd_date=C2=A0 |=C2=A0 description=C2=A0 | notes
----------+----------------+------------+------------+---------------+-----= --
=C2=A0 4242.01=C2=A0 | Expert witness | 2008-10-15 | 2008-10-28 | Consol En= ergy |
=C2=A0 4242.01=C2=A0 | Expert witness | 2008-10-15 | 2008-10-28 | Consol En= ergy |
=C2=A0 4242.01=C2=A0 | Expert witness | 2008-10-15 | 2008-10-28 | Consol En= ergy |
=C2=A0 4242.01=C2=A0 | Expert witness | 2008-10-15 | 2008-10-28 | Consol En= ergy |
(4 rows)

How do I clean this up so there's only a single row for this project nu= mber?

TIA,

Rich


--00000000000022df220621e43ad2--