[英]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
Let's say I have a table that consists of just employee name and the date he/she started.假设我有一张表,其中仅包含员工姓名和他/她的入职日期。 I want to delete all records in the table that are less than the last year's current date, but not delete the end dates for the given months in the past two years of the current date.我想删除表中所有小于去年当前日期的记录,但不删除当前日期过去两年中给定月份的结束日期。
For example - if the current date was '29-SEP-2020'例如 - 如果当前日期是“29-SEP-2020”
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
The expected records to be deleted from my statement should be从我的声明中删除的预期记录应该是
Adam 28-SEP-2019
Melanie 27-SEP-2018
Isaac 30-SEP-2017
Again, please note that 30-SEP-2019 and 30-SEP-2018 won't be deleted because they are still the end dates in the range for the last two years of 2020 of September.再次请注意,30-SEP-2019 和 30-SEP-2018 不会被删除,因为它们仍然是 2020 年最后两年的 9 月范围内的结束日期。 30-SEP-2017 records will be deleted because it is outside that range. 30-SEP-2017 记录将被删除,因为它超出了该范围。 For all of the daily dates, those records will be deleted from the last year's current date.对于所有每日日期,这些记录将从去年的当前日期中删除。
This will do it:这将做到:
DELETE FROM tablename
WHERE DateStarted < ADD_MONTHS(sysdate, -12)
AND DateStarted <> LAST_DAY(ADD_MONTHS(sysdate, -24))
The condition to not delete the end date of the current month of the last year is covered by the 1st condition of the WHERE clause. WHERE 子句的第一个条件涵盖了不删除去年当月结束日期的条件。
See the demo .请参阅演示。
Results (remaining rows):结果(剩余行):
> EMP | DATESTARTED
> :----- | :----------
> John | 01-SEP-20
> Jane | 29-SEP-19
> Lauren | 30-SEP-19
> Caleb | 30-SEP-18
The records you want deleted correspond to:您要删除的记录对应于:
select t.*
from t
where date_started >= add_months(sysdate, -24);
You can turn this easily into a delete
statement, if you really want to delete them.如果你真的想删除它们,你可以很容易地将它变成一个delete
语句。
You explain a lot about end of months and stuff, but that doesn't seem relevant.你解释了很多关于月末之类的事情,但这似乎并不相关。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.