[英]Find closest, future date of given number of weeks cycle
Let's say I have a:假设我有一个:
start_date = '2020-01-06'
and date(now())
和
date(now())
And I want to find closest future date, starting from now that is a multiplication of 4 (n) (or another number) weeks cycle from start_date
我想找到最接近的未来日期,从现在开始,从
start_date
开始乘以 4 (n)(或其他数字)周周期
Expected output as for today would be '2020-03-02'
今天的预期产量为
'2020-03-02'
I can't came up with any smart idea to check for it.我想不出任何聪明的主意来检查它。
I'll highly appreciate any suggestions.我将非常感谢任何建议。
You could do date arithmetics:你可以做日期算术:
In SQL:在 SQL 中:
date'2020-01-06' + ceil((current_date - date'2020-01-06')::numeric / 4 / 7)::int * 4 * 7
Dremo on DB Fiddlde : DB Fiddlde 上的 Dremo :
with t as (select date'2020-01-06' anchor, 4 n)
select
anchor,
n,
current_date,
anchor + ceil((current_date - anchor)::numeric / n / 7)::int * n * 7 next_date
from t
anchor | n | current_date | next_date :--------- | -: | :----------- | :--------- 2020-01-06 | 4 | 2020-02-10 | 2020-03-02
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.