计算Postgres最近的工作日 [英] Calculate closest working day in Postgres

查看:135
本文介绍了计算Postgres最近的工作日的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要根据订单的请求交货日期安排postgres查询中的某些项目。因此,例如,订单在星期一(例如20120319)有请求的交货,订单需要在上一个工作日(20120316)准备。

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 for example), and the order needs to be prepared on the prior working day (20120316).

想法最直接的方法?我可以添加日期表。我认为有一个比一套长的case语句更好的方法:
SELECT EXTRACT(DOW FROM TIMESTAMP'2001-02-16 20:38:40');

Thoughts on the most direct method? I'm open to adding a dates table. I'm thinking there's got to be a better way than a long set of case statements using: SELECT EXTRACT(DOW FROM TIMESTAMP '2001-02-16 20:38:40');

推荐答案

要有上一个工作日:

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.

这篇关于计算Postgres最近的工作日的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆