删除 table 中所有早于去年当前日期的记录,但不删除过去 2 年给定月份的结束日期

Delete all records in the table that are less than the last year's current date, but don't delete the end dates for the given months for past 2 years

假设我有一个 table,其中仅包含员工姓名和 he/she 开始日期。我想删除table中所有小于去年当前日期的记录,但不删除当前日期过去两年中给定月份的结束日期。

例如 - 如果当前日期是“2020 年 9 月 29 日”

Emp     Date-Started
---     ------------
John    01-SEP-2020
Jane    29-SEP-2019
Adam    28-SEP-2019
Lauren  30-SEP-2019
Caleb   30-SEP-2018
Melanie 27-SEP-2018
Isaac   30-SEP-2017

从我的语句中删除的预期记录应该是

Adam    28-SEP-2019
Melanie 27-SEP-2018
Isaac   30-SEP-2017

再次请注意,30-SEP-2019 和 30-SEP-2018 不会被删除,因为它们仍然是 2020 年最后两年的 9 月范围内的结束日期。 30-SEP-2017 记录将被删除,因为它超出了该范围。对于所有每日日期,这些记录将从去年的当前日期中删除。

您要删除的记录对应于:

select t.*
from t
where date_started >= add_months(sysdate, -24);

如果你真的想删除它们,你可以很容易地将它变成一个 delete 语句。

你解释了很多月末之类的东西,但这似乎不相关。

这样做就可以了:

DELETE FROM tablename
WHERE DateStarted < ADD_MONTHS(sysdate, -12) 
AND DateStarted <> LAST_DAY(ADD_MONTHS(sysdate, -24))

不删除上一年当月结束日期的条件包含在WHERE子句的第一个条件中。

参见demo
结果(剩余行):

> EMP    | DATESTARTED
> :----- | :----------
> John   | 01-SEP-20  
> Jane   | 29-SEP-19  
> Lauren | 30-SEP-19  
> Caleb  | 30-SEP-18