简体   繁体   English

根据间隔查找日期

[英]find the date based on interval

Below is my query.以下是我的查询。

DECLARE startdate:='12-04-2019', interval  =1 OR 3

SELECT date(d) as dt,0 as flag
FROM generate_series(date '2019-04-01', date '2020-03-31', interval '1 day') d
ORDER BY date(d)

Ex: If interval is 1 month, flag to be updated as 1 below records and rest for 0例如:如果间隔为 1 个月,则标志将在记录下方更新为 1,rest 为 0

    date         flag
    01-04-2019    0
    .             0
    .             0
    12-05-2019    1
    .             0
    .             0  
    12-06-2019    1
    .             0
    .             0
    12-07-2019    1
    ..            0
    31-04-2020    0

If interval is 3, flag to be updated as 1 for quarter如果间隔为 3,则标志将在季度更新为 1

      date        flag
    01-04-2019    0
    .             0
    .             0
    12-07-2019    1
    .             0
    .             0  
    12-10-2019    1
    .             0
    .             0
    12-01-2020    1
    .             0
    31-04-2020    0

I am really stuck trying get the result.我真的很难得到结果。

I'm not sure what the declare is for.我不确定declare的用途。 Is this a programming block?这是一个编程块吗?

In any case, you can multiple the interval by a constant.在任何情况下,您都可以将interval乘以一个常数。 So:所以:

SELECT d as dt, 0 as flag
FROM generate_series(date '2019-04-01',
                     date '2020-03-31',
                     4 * interval '1 month'
                    ) d
ORDER BY d

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

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