Calculate closest working day in Postgres

后端 未结 4 1077
没有蜡笔的小新
没有蜡笔的小新 2021-01-05 13:24

I need to schedule some items in a postgres query based on a requested delivery date for an order. So for example, the order has a requested delivery on a Monday (20120319

4条回答
  •  说谎
    说谎 (楼主)
    2021-01-05 13:59

    To have the previous work day:

    select max(s.a) as work_day
    from (
        select s.a::date
        from generate_series('2012-01-02'::date, '2050-12-31', '1 day') s(a)
        where extract(dow from s.a) between 1 and 5
        except
        select holiday_date
        from holiday_table
        ) s
    where s.a < '2012-03-19'
    ;
    

    If you want the next work day just invert the query.

提交回复
热议问题