Re: Delete Question

From: hubert depesz lubaczewski <depesz(at)gmail(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:27:57
Message-ID: 9e4684ce0512062327t728e1873pf308f10fcee0406e@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 12/7/05, Alex <alex(at)meerkatsoft(dot)com> wrote:
>
> I have a table where I store changes made to an order. The looks like
> ProdID, ChangeDate, Change1, Change2, ... etc.
> Some ProdIDs have multiple records.
> 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?
>

delete from table_name where exists (select * from table_name x where
x.prodid = table_name.prodid and x.changedate > table_name.changedate);

this should work.

depesz

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Arnulf Christl 2005-12-07 07:30:26 Re: [Mapbender-users] Re: [Freegis-list] Re: [GENERAL]
Previous Message Emil Rachovsky 2005-12-07 07:26:01 Re: [SQL] lost in system tables