如何计算oracle中的商业日,不包括一周中的任何特定日期?

时间:2018-03-12 20:20:41

标签: sql oracle11g

在我的数据中,我有一个开始日和结束日。我想计算这两天之间的差异。但我必须永远排除周三和周五。我仍然试图自己找到解决方案。如果有人知道怎么做,请帮忙。这里有一些预构建代码可用于实现:

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; 

1 个答案:

答案 0 :(得分: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;