假设我有一张记录价格历史的表格,例如:
_______________________________________________________
|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 |
|--------------------------------------------------------
我想删除超过 2 年的条目,除非最新条目超过 2 年。很难用语言表达,基本上我想结束这个:
_______________________________________________________
|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 |
|--------------------------------------------------------
因此查询将删除马自达超过两年的条目,以及本田和讴歌超过两年的条目,最新的除外。
只有在价格变化时才会将条目插入表中,因此如果价格在 5 年内没有变化,则条目将是 5 年,但这并不意味着数据不准确。