美文网首页
postgres里建立工作日表

postgres里建立工作日表

作者: 渣渣曦 | 来源:发表于2020-04-16 17:05 被阅读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即可准确的算出工作日

    相关文章

      网友评论

          本文标题:postgres里建立工作日表

          本文链接:https://www.haomeiwen.com/subject/ueldvhtx.html