mysql - Query to delete old records, with an exception -
lets have table keeps history of prices, example:
_______________________________________________________ |make | model | year | price | date_of_record | |-------------------------------------------------------- |mazda | 6 | 2008 | 10,000 | 2013-05-13 00:00:00 | |mazda | 6 | 2008 | 11,000 | 2012-05-13 00:00:00 | |mazda | 6 | 2008 | 12,000 | 2011-05-13 00:00:00 | |mazda | 6 | 2008 | 11,750 | 2010-05-13 00:00:00 | |honda | civic | 2000 | 3,000 | 2011-05-13 00:00:00 | |honda | civic | 2000 | 4,000 | 2010-05-13 00:00:00 | |honda | civic | 2000 | 5,000 | 2009-05-13 00:00:00 | |honda | civic | 2000 | 5,050 | 2008-05-13 00:00:00 | |acura | tl | 2009 | 21,000 | 2010-05-13 00:00:00 | |--------------------------------------------------------
i want delete entries more 2 years old unless latest entry more 2 years old. hard put words, want end this:
_______________________________________________________ |make | model | year | price | date_of_record | |-------------------------------------------------------- |mazda | 6 | 2008 | 10,000 | 2013-05-13 00:00:00 | |mazda | 6 | 2008 | 11,000 | 2012-05-13 00:00:00 | |honda | civic | 2000 | 3,000 | 2011-05-13 00:00:00 | |acura | tl | 2009 | 21,000 | 2010-05-13 00:00:00 | |--------------------------------------------------------
so query delete entries more 2 years old mazda, , entries more 2 years old honda , acura, exception of latest.
entries inserted table when price changes, if price hasn't changed in 5 years entry 5 years old, not mean data not accurate.
first, lets list of car, make, model, year , respective latest "date of record". so, have 2007 vehicle , 2008 of same make/model each latest date entry of 2011-01-01. want keep vehicle entry both 2007 , 2008 models however, example showed old dates want keep retained data honda civic , acural tl respectively.
run this, confirm results
select c.`make`, c.`model`, c.`year`, max( c.date_of_record ) latestrecorddate yourcarpricetable c group c.`make`, c.`model`, c.`year` having max( c.date_of_record ) < date_add(curdate(),interval -2 year)
now, use basis delete current table via left join on same make, model , year. 2 conditions allow delete.
1) there no match found in "keepthese" subquery , latest date of record more 2 years old...
2) there match found in "keepthese" subquery , date older recent qualified (too old) result set of keepthese.
per data example, "keepthese" result set should include
make model year latestrecorddate honda civic 2000 2011-05-13 00:00:00 acura tl 2009 2010-05-13 00:00:00
so, mazda 6 delete 2 entries 2010 , 2011 date entries since make/model/year not included in list (via test null).
your honda civic delete entries older it's latest "keepthese" value of 2011-05-13, , delete 2008, 2009 , 2010 entries.
your acura tl not have older it's single entry, left alone.
delete ypt.* yourcarpricetable ypt left join ( select c.`make`, c.`model`, c.`year`, max( c.date_of_record ) latestrecorddate yourcarpricetable c group c.`make`, c.`model`, c.`year` having max( c.date_of_record ) < date_add(curdate(),interval -2 year) ) keepthese on ypt.`make` = keepthese.`make` , ypt.`model` = keepthese.`model` , ypt.`year` = keepthese.`year` ( keepthese.`make` null , ypt.date_of_record < date_add(curdate(),interval -2 year)) or ( ypt.`make` = keepthese.`make` , ypt.`model` = keepthese.`model` , ypt.`year` = keepthese.`year` , ypt.date_of_record < keepthese.latestrecorddate )
the subquery processed once front, left-joined delete version of car/price table.
Comments
Post a Comment