Re: Delete Question

From: Michael Glaesemann <grzm(at)myrealbox(dot)com>
To: Alex <alex(at)meerkatsoft(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Delete Question
Date: 2005-12-07 07:35:10
Message-ID: 51E90DAD-568E-47EC-B047-6BA02977DD4B@myrealbox.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Paolo Cavallini 2005-12-07 07:38:23 Re: [Mapbender-users] Re: [Freegis-list] Re: [GENERAL] Map of Postgresql Users (OT)
Previous Message Arnulf Christl 2005-12-07 07:30:26 Re: [Mapbender-users] Re: [Freegis-list] Re: [GENERAL]