sql - Delete all duplicate records from Oracle table except oldest -


i have 2 tables, 1 parent tablea , 1 child tableb. tableb has 1 or more records parent record in tablea. need delete records tableb except earliest date i.e. duplicates in tableb. don't think tablea needs involved in statement i'm including reference.

tablea  _______  secid, secname  1,     sec1 2,     sec2 3,     sec3 4,     sec4  tableb _________ incid, secid, paydate  16,    1,     11/03/2011 17,    1,     11/04/2011 18,    2,     10/01/2011 19,    3,     01/06/2011 20,    3,     01/09/2011 21,    3,     01/12/2011 22,    4,     10/06/2011 

so in tableb above need delete records 17, 20, , 21 leaving 1 record each secid. far have below reason it's including earliest record want keep:

delete tableb paydate not in (   select min(paydate)from tableb   having ( count(paydate) > 1 ) ) 

you can use rowid , analytics:

sql> delete tableb   2   rowid not in   3           (select first_value(rowid)over(partition secid order paydate)   4              tableb);  3 rows deleted  sql> select * tableb;       incid      secid paydate ---------- ---------- -----------         16          1 11/03/2011         18          2 10/01/2011         19          3 01/06/2011         22          4 10/06/2011 

you use more conventional semi-join:

sql> delete tableb b_out   2   exists (select null   3                   tableb b_in   4                  b_in.secid = b_out.secid   5                    , b_in.paydate < b_out.paydate);  3 rows deleted 

Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -