Re: Delete Question - Mailing list pgsql-general

From Michael Glaesemann
Subject Re: Delete Question
Date
Msg-id 51E90DAD-568E-47EC-B047-6BA02977DD4B@myrealbox.com
Whole thread Raw
In response to Delete Question  (Alex <alex@meerkatsoft.com>)
List pgsql-general
On Dec 7, 2005, at 16:21 , Alex wrote:

> Is there an easy way to delete all records of a ProdID except the
> most recent (ChangeDate is timestamp) one? Preferably in one SQL
> statement?

Here's one way to do it, though not it one SQL statement:

create table copy_of_original_table as
select distinct on ("ProdID") "ProdID", ...
from original_table
order by "ChangeDate" desc;

truncate original_table;

insert into original_table ("ProdID", ... )
select "ProdID", ...
from copy_of_original_table;

Note you need to quote the column names if case is important.

Michael Glaesemann
grzm myrealbox com




pgsql-general by date:

Previous
From: hubert depesz lubaczewski
Date:
Subject: Re: Delete Question
Next
From: Emil Rachovsky
Date:
Subject: Re: [SQL] lost in system tables