简体   繁体   English

删除表中所有小于去年当前日期的记录,但不删除过去 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

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.

相关问题 查询小于给定日期(月和年)的所有日期 - Query all dates less than the given date (Month and Year) 如何删除特定表的所有记录,但最近 6 个月的数据除外 - How to delete all the records a particular table, but except last 6 months data 我想删除超过 6 年加上当前财政年度的记录 - I want to delete records older than 6 years plus current financial year 上一季度的最后一个值,因为它的日期小于或等于当前行的日期-SQL - Last value from previous quarter, given that it's date is less than or equal the current rows date - SQL 如何从目标表中删除超过 4 年的记录 - How to delete records from the target table which is older than 4 years 获取最近 2 个月(当年)和上个月(去年)的记录 - Get records of last 2 months (current year), and last month(last year) SQL Server Delete记录大于去年的这个时间 - SQL Server Delete records greater than this time last year SQL中的t.date属性小于当前年份 - Sql where t.date attribute is less than current year Select 针对给定年份的所有月份的记录形成一个表,其中记录来自第二个表 - Select Records against all months of a given year form one table with records from second table 使用查询删除比给定日期早几天的日期 - Using a query to delete dates a number of days older than a given date
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM