Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.misc -> Re: When duplicate delete oldest date and if equal delete either?

Re: When duplicate delete oldest date and if equal delete either?

From: Thorsten Kettner <thorsten.kettner_at_web.de>
Date: Fri, 14 Sep 2007 05:22:42 -0700
Message-ID: <1189772562.525681.117900@r34g2000hsd.googlegroups.com>


On 14 Sep., 13:32, jobs <j..._at_webdos.com> wrote:
> select * from tmp_autorecharge_work a where exists
> (select l.account_number from tmp_autorecharge_work l where
> a.account_number = l.account_number
> group by l.account_number having count(*) > 1)
> and a.last_update = (select min(b.last_update) from
> tmp_autorecharge_work b where b.account_number = a.account_number)
>
> The above logic does list the older record, problem is in some cases
> there are two or more older records with the same date.
>
> Basically I only want to leave the most recent row in the table, if
> there are two rows with the same last_update, remove the first in. The
> above is a select I planned to turn into a delete statement.
>
> thanks for any help or information.

If I understand correctly, you want to delete all records that are not the most recent entries for an account number? Then it would look thus:

delete from tmp_autorecharge_work
where (account_number, last_update) not in   (select account_number, max(last_update) from tmp_autorecharge_work group by account_number) Received on Fri Sep 14 2007 - 07:22:42 CDT

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US