| From: | Muhammad Usman Khan <usman(dot)k(at)bitnine(dot)net> | 
|---|---|
| To: | Rich Shepard <rshepard(at)appl-ecosys(dot)com> | 
| Cc: | pgsql-general(at)postgresql(dot)org | 
| Subject: | Re: Removing duplicate rows in table | 
| Date: | 2024-09-12 04:06:00 | 
| Message-ID: | CAPnRvGs1+uqWe1ny0jMJapYMNR+i=jDvWRL4YwsgsUdAQFSNGg@mail.gmail.com | 
| Views: | Whole Thread | Raw Message | Download mbox | Resend email | 
| Thread: | |
| Lists: | pgsql-general | 
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 <rshepard(at)appl-ecosys(dot)com>
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
>
>
>
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Muhammad Usman Khan | 2024-09-12 04:35:38 | Re: Recommendations on improving the insert on conflict do nothing performance | 
| Previous Message | Andreas Joseph Krogh | 2024-09-12 03:41:03 | Effects of REVOKE SELECT ON ALL TABLES IN SCHEMA pg_catalog FROM PUBLIC |