简体   繁体   中英

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

    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

      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. Is this a programming block?

In any case, you can multiple the interval by a constant. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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