Re: Deleting one of 2 identical records

From: "David Johnston" <polobo(at)yahoo(dot)com>
To: "'Andy Colson'" <andy(at)squeakycode(dot)net>, "'Thom Brown'" <thom(at)linux(dot)com>
Cc: "'Gauthier, Dave'" <dave(dot)gauthier(at)intel(dot)com>, <pgsql-general(at)postgresql(dot)org>
Subject: Re: Deleting one of 2 identical records
Date: 2011-09-06 22:01:41
Message-ID: 011401cc6ce0$90762e00$b1628a00$@yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

-----Original Message-----
From: pgsql-general-owner(at)postgresql(dot)org
[mailto:pgsql-general-owner(at)postgresql(dot)org] On Behalf Of Andy Colson
Sent: Tuesday, September 06, 2011 1:55 PM
To: Thom Brown
Cc: Gauthier, Dave; pgsql-general(at)postgresql(dot)org
Subject: Re: [GENERAL] Deleting one of 2 identical records

I wonder.. using the new writeable cte's, could you:

with x (
-- id = 5 has two identical rows, but limit 1
select * from orig where id = 5 limit 1;
)
delete from x;

-Andy

----------------------------------------------------------

By my understanding it is a writeable CTE because the statement inside the
CTE can now be INSERT/UPDATE/DELETE (in addition to the SELECT - read only -
statement).

A CTE is, in some ways, like an immediately materialized view. Any reference
to it does not affect the source tables; thus your example likely would not
work. It isn't a RULE where "x" is simply an alias for "orig".

The real problem is not the language but the table design. The idea of
"true duplicates" is generally problematic but when it does occur it is
advisable to introduce some kind of artificial key/sequence to allow for
direct selection of the row without resorting to internals. Then, it is
simply to use the full power of the SELECT statement (with Window functions
and CTEs) to identify the rows that are to be deleted and feed the PK from
those rows into the DELETE's WHERE clause using a sub-query.

David J.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Mike Orr 2011-09-06 22:03:25 Complex query question
Previous Message Merlin Moncure 2011-09-06 21:23:42 Re: conditional insert