计算两个日期之间的天数,不包括周末

时间:2018-11-12 17:31:35

标签: sql oracle oracle11g

如何计算两个日期之间的天数,不包括周末?

我有以下数据,仅通过执行ClosingDate - ActivityDate就可以得到TAT值。但是,我不想考虑周六/周日。

+-------------------------+--------+-------------------------+------+
|      ActivityDate       | TypeID |       ClosedDate        | TAT  |
+-------------------------+--------+-------------------------+------+
| Wednesday, May 02, 2018 |   2502 | Wednesday, May 09, 2018 | 7.00 |
| Monday, May 07, 2018    |   2503 | Thursday, May 10, 2018  | 3.00 |
| Tuesday, May 08, 2018   |   2504 | Friday, May 11, 2018    | 3.00 |
| Wednesday, May 09, 2018 |   2505 | Thursday, May 10, 2018  | 1.00 |
| Thursday, May 10, 2018  |   2506 | Friday, May 11, 2018    | 1.00 |
| Friday, May 11, 2018    |   2507 | Thursday, May 17, 2018  | 6.00 |
| Thursday, May 10, 2018  |   2508 | Tuesday, May 15, 2018   | 5.00 |
| Monday, May 14, 2018    |   2509 | Wednesday, May 16, 2018 | 2.00 |
| Monday, May 14, 2018    |   2510 | Thursday, May 17, 2018  | 3.00 |
+-------------------------+--------+-------------------------+------+

非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

这样的事情怎么样?

我只提取了一部分数据。这个想法是:检查天数(to_char(date, 'd'));如果活动日期(AD)的天数大于关闭日期(CD),则再删除两天(周末)。

如果其中一个(或两个)日期都在星期六或星期日(但是根据您的示例数据,它们不是),则可能需要对此进行调整。

SQL> with test (ad, cd) as
  2    (select date '2018-05-02', date '2018-05-09' from dual union all
  3     select date '2018-05-11', date '2018-05-17' from dual union all
  4     select date '2018-05-10', date '2018-05-15' from dual
  5    ),
  6  day_num as
  7    (select ad, cd,
  8      to_char(ad, 'd') addn, to_char(cd, 'd') cddn,
  9      to_char(ad, 'dy') ady, to_char(cd, 'dy') cdy
 10     from test
 11    )
 12  select ad, cd,
 13    cd - ad - case when addn > cddn then 2 else 0 end diff
 14  from day_num;

AD              CD                    DIFF
--------------- --------------- ----------
02.05.2018, wed 09.05.2018, wed          7
11.05.2018, fri 17.05.2018, thu          4
10.05.2018, thu 15.05.2018, tue          3

SQL>