【原创】如何使用一句SQL计算工作日天数?

现在有这样一个需求,要求计算两个日期间的工作日天数,要求除去节假日,其中节假日有一张配置表,具体的格式如下:

开始日期 结束日期 节假日类型 节假日名称
2013-08-10   2013-08-12      法定        端午节
2013-01-01 2013-01-03      法定      元旦

要求计算两个日期间的天数时要将周末时间去掉,还要将节假日区间的天数排除:

select count(*)
  from (select to_date('2010-01-01', 'yyyy-mm-dd') + LEVEL - 1 as days
          from dual
        CONNECT BY to_date('2010-01-01', 'yyyy-mm-dd') + LEVEL <=
                   to_date('2014-01-01', 'yyyy-mm-dd') + 1)
 WHERE trim(to_char(days, 'D')) IN ('1', '2', '3', '4 ', '5')
   and to_char(days, 'yyyy-mm-dd') not in
       (select to_char(tt, 'yyyy-mm-dd')
          from (select distinct d_begin + LEVEL - 1 as tt
                  from t_pz
                CONNECT BY d_begin + LEVEL <= d_end + 1))

如果有节假日的配置,则在子查询中过滤掉即可。 

原文地址:https://www.cnblogs.com/zhangxsh/p/3499032.html