如何在 oracle 中计算工作日,不包括一周中的任何特定日期?
Posted
技术标签:
【中文标题】如何在 oracle 中计算工作日,不包括一周中的任何特定日期?【英文标题】:how to calculate business Day in oracle excluding any specific day of the week? 【发布时间】:2018-03-12 20:20:41 【问题描述】:在我的数据中,我有一个开始日和结束日。我正在尝试计算这两天之间的差异。但我必须始终排除周三和周五。我仍在尝试自己找到解决方案。如果有人知道该怎么做,请提供帮助。以下是一些可用于实验的预构建代码:
SELECT
START_DATE
,END_DATE
,TRUNC(START_DATE, 'D') AS TRUNC_START
,TRUNC(END_DATE, 'D') AS TRUNC_END
,to_char(START_DATE, 'DY') AS START_DAY_NAME
,to_char(END_DATE, 'DY') AS END_DATE_NAME
,TRUNC(END_DATE) - TRUNC(START_DATE) + 1 AS TOTAL_RESOLVE_TIME
,TRUNC((TRUNC(END_DATE) - TRUNC(START_DATE) + 1)/7*2) AS NO_OF_WEEK
FROM
(
SELECT
TO_DATE('2018-03-08', 'YYYY-MM-DD') AS START_DATE
,TO_DATE('2018-03-14', 'YYYY-MM-DD') AS END_DATE
FROM dual
UNION ALL
SELECT
TO_DATE('2018-03-09', 'YYYY-MM-DD') AS START_DATE
,TO_DATE('2018-03-15', 'YYYY-MM-DD') AS END_DATE
FROM dual
UNION ALL
SELECT
TO_DATE('2018-03-02', 'YYYY-MM-DD') AS START_DATE
,TO_DATE('2018-03-11', 'YYYY-MM-DD') AS END_DATE
FROM dual
UNION ALL
SELECT
TO_DATE('2018-03-01', 'YYYY-MM-DD') AS START_DATE
,TO_DATE('2018-03-17', 'YYYY-MM-DD') AS END_DATE
FROM dual) a;
【问题讨论】:
您可以在 this 的基础上计算周五和周三的数量,然后从总天数中减去 @wolfrevokcats 这真的很有帮助 非常感谢。我想我现在可以了。 【参考方案1】:在阅读了@wolfrevokcats 提到的上述链接后,它现在可以工作了。
SELECT
START_DATE
,END_DATE
,TRUNC(START_DATE, 'D') AS TRUNC_START
,TRUNC(END_DATE, 'D') AS TRUNC_END
,to_char(START_DATE, 'DY') AS START_DAY_NAME
,to_char(END_DATE, 'DY') AS END_DATE_NAME
-- Find the difference between Resolve and Create Date
,TRUNC(END_DATE) - TRUNC(START_DATE) + 1 AS TOTAL_RESOLVE_TIME
-- Find the number of Fridays in the whole range
,((next_day(END_date-7,'FRI')-next_day(START_DATE-1,'FRI'))/7)+1 AS NO_OF_FRI
-- Find the number of Wednesday in the whole range
,((next_day(END_date-7,'WED')-next_day(START_DATE-1,'WED'))/7)+1 AS NO_OF_WED
-- Applying the above finding the total business hours
,(TRUNC(END_DATE) - TRUNC(START_DATE) + 1)
- (((next_day(END_date-7,'FRI')-next_day(START_DATE-1,'FRI'))/7)+1)
- (((next_day(END_date-7,'WED')-next_day(START_DATE-1,'WED'))/7)+1) AS TOTAL_BUSINESS_HOUR
FROM
(
SELECT
TO_DATE('2018-03-08', 'YYYY-MM-DD') AS START_DATE
,TO_DATE('2018-03-14', 'YYYY-MM-DD') AS END_DATE
FROM dual
UNION ALL
SELECT
TO_DATE('2018-03-09', 'YYYY-MM-DD') AS START_DATE
,TO_DATE('2018-03-15', 'YYYY-MM-DD') AS END_DATE
FROM dual
UNION ALL
SELECT
TO_DATE('2018-03-02', 'YYYY-MM-DD') AS START_DATE
,TO_DATE('2018-03-11', 'YYYY-MM-DD') AS END_DATE
FROM dual
UNION ALL
SELECT
TO_DATE('2018-03-01', 'YYYY-MM-DD') AS START_DATE
,TO_DATE('2018-03-17', 'YYYY-MM-DD') AS END_DATE
FROM dual) a;
【讨论】:
以上是关于如何在 oracle 中计算工作日,不包括一周中的任何特定日期?的主要内容,如果未能解决你的问题,请参考以下文章