postgres里建立工作日表

2020-04-16  本文已影响0人  渣渣曦

首先创建工作日历表

create table calendar_date (
  calendar_day   date not null primary key,
  week_number int,
  is_working_day char(1) not null
);

2020年每一天插入表中

insert into calendar_date(calendar_day,week_number,is_working_day) SELECT the_day,extract('ISODOW' FROM the_day),'Y'
FROM   generate_series(timestamp '2020-01-01'
                     , timestamp '2020-12-31'
                     , interval  '1 day') the_day

变更周六、日为非工作日

update calendar_date set is_working_day='N' where week_number>5;

查询5个工作日前的日期

select calendar_day from calendar_date where calendar_day<=current_date and is_working_day='Y' order by calendar_day desc offset 5 limit 1

查询出calendar_day后,所有小于该日期的记录都是超出5个工作日的记录。
可以按照国家发布的节假日,调整该表中的is_working_day即可准确的算出工作日

上一篇下一篇

猜你喜欢

热点阅读