栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在给定日期增加工作日数

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在给定日期增加工作日数

关键是生成一系列工作日,并用

row_number()
以下编号对它们进行编号:

create or replace function add_business_day(from_date date, num_days int)returns dateas $fbd$    select d    from (        select d::date, row_number() over (order by d)        from generate_series(from_date+ 1, from_date+ num_days* 2+ 5, '1d') d        where  extract('dow' from d) not in (0, 6)  and d not in (select pyha from pyha)        ) s    where row_number = num_days$fbd$ language sql;

测试查询的结果似乎正确:

select days, add_business_day('2018-12-08', days)from generate_series(1, 20) days days | add_business_day ------+------------------    1 | 2018-12-10    2 | 2018-12-11    3 | 2018-12-12    4 | 2018-12-13    5 | 2018-12-14    6 | 2018-12-17    7 | 2018-12-18    8 | 2018-12-19    9 | 2018-12-20   10 | 2018-12-21   11 | 2018-12-27   12 | 2018-12-28   13 | 2018-12-31   14 | 2019-01-02   15 | 2019-01-03   16 | 2019-01-04   17 | 2019-01-07   18 | 2019-01-08   19 | 2019-01-09   20 | 2019-01-10(20 rows)

或者,您可以在循环中找到日期:

create or replace function add_business_day_loop(from_date date, num_days int)returns dateas $fbd$begin    while num_days > 0 loop        from_date:= from_date+ 1;        while from_date in (select pyha from pyha) or extract('dow' from from_date) in (0, 6) loop from_date:= from_date+ 1;        end loop;        num_days:= num_days- 1;    end loop;    return from_date;end;$fbd$ language plpgsql;


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/431727.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号