简体   繁体   English

根据时间间隔使用 sysdate 更新日期列

[英]Update date column with sysdate based on the interval

I need to create a plsql script that must update the population_date column with sysdate based on the population interval (hourly, daily, weekly, monthly) when executed.我需要创建一个 plsql 脚本,该脚本必须在执行时根据人口间隔(每小时、每天、每周、每月)使用 sysdate 更新 population_date 列。

For example, if it is a weekly population interval, the existing date in the population_date column must be updated as follows:例如,如果它是每周人口间隔,则必须更新 population_date 列中的现有日期,如下所示:

Rec_sid (existing date) Population_date Rec_sid(现有日期) Population_date

1 20-jan-2020 sysdate(03-aug-2020) 1 2020 年 1 月 20 日系统日期(2020 年 8 月 3 日)

2 20-jan-2020 sysdate (03-aug-2020) 2 2020 年 1 月 20 日系统日期(2020 年 8 月 3 日)

3 19-jan-2020 sysdate-1(02-aug-2020) 3 2020 年 1 月 19 日 sysdate-1(2020 年 8 月 2 日)

4 19-jan-2020 sysdate-1(02-aug-2020) 4 2020 年 1 月 19 日 sysdate-1(2020 年 8 月 2 日)

5 18-jan-2020 sysdate-2(01-aug-2020) 5 2020 年 1 月 18 日系统日期 2(2020 年 8 月 1 日)

6 18-jan-2020 sysdate-2(01-aug-2020) 6 2020 年 1 月 18 日系统日期 2(2020 年 8 月 1 日)

7 17-jan-2020 sysdate-3 (31-jul-2020) 7 2020 年 1 月 17 日 sysdate-3(2020 年 7 月 31 日)

8 17-jan-2020 sysdate-3(31-jul-2020) 8 2020 年 1 月 17 日 sysdate-3(2020 年 7 月 31 日)

9 17-jan-2020 sysdate-3(31-jul-2020) 9 2020 年 1 月 17 日 sysdate-3(2020 年 7 月 31 日)

10 16-jan-2020 sysdate-4(30-jul-2020) 10 2020 年 1 月 16 日 sysdate-4(2020 年 7 月 30 日)

11 16-jan-2020 sysdate-4)30-jul-2020) 11 2020 年 1 月 16 日系统日期 4)2020 年 7 月 30 日)

12 15-jan-2020 sysdate-5(29-jul-2020) 12 2020 年 1 月 15 日 sysdate-5(2020 年 7 月 29 日)

13 15-jan-2020 sysdate-5(29-jul-2020) 13 2020 年 1 月 15 日 sysdate-5(2020 年 7 月 29 日)

14 14-jan-2020 sysdate-6(28-jul-2020) 14 2020 年 1 月 14 日 sysdate-6(2020 年 7 月 28 日)

And this column exists in 100 tables with different structure and the population_date column alone must be updated with the current date based on the interval.并且该列存在于 100 个不同结构的表中,仅 population_date 列必须根据时间间隔更新为当前日期。

If I understand correctly, you want the most recent date on the same day of the week on or before today.如果我没理解错的话,您希望最近的日期是在今天或之前的一周中的同一天。 That logic would be:该逻辑将是:

population_date + floor( (sysdate - population_date) / 7) * 7

You can put this into an update as:您可以将其放入update中:

update t
    set population_date = population_date + floor( (sysdate - population_date) / 7) * 7;

I would advise you to run a select first to validate the results.我建议您先运行select来验证结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM