"일꾼이 일을 잘하려면 먼저 도구를 갈고 닦아야 한다." - 공자, 『논어』.
첫 장 > 프로그램 작성 > Oracle SQL의 두 날짜 사이의 영업 시간을 계산하는 방법은 무엇입니까?

Oracle SQL의 두 날짜 사이의 영업 시간을 계산하는 방법은 무엇입니까?

2025-02-06에 게시되었습니다
검색:946

How to Calculate Business Hours Between Two Dates in Oracle SQL?

비즈니스 시간 사양

는 월요일부터 토요일까지 8:00입니다. 오전 6 시부 터 오후 6시. 이 시간을 계산에 통합하려면 날짜 조작과 조건부 로직의 조합을 사용할 수 있습니다.

SQL 쿼리

다음 SQL 쿼리는 시간 계산을위한 솔루션을 제공합니다. 지정된 비즈니스 시간을 기준으로 : 작업 선택, start_time, end_time, 둥근( ( - ISO 주가 시작된 전체 몇 주 차이를 계산하십시오. (trunc (end_time, 'iw') - trunc (start_time, 'iw')) * (10/24) * (6/7) - 마지막 주에 하루 종일 추가하십시오. 최소 (trunc (end_time) -trunc (end_time, 'iw'), 6) * (10/24) - 시작 날짜 전 요일부터 하루 종일 며칠을 빼십시오. - 최소 (trunc (start_time) -trunc (start_time, 'iw'), 6) * (10/24) - 마지막 날의 시간을 추가하십시오 최소 (가장 큰 (End_Time -Trunc (end_time) -8/24, 0), 10/24) - 범위가 시작되기 전날 시간을 빼십시오. - 최소 (Greatest (start_time -trunc (start_time) -8/24, 0), 10/24) )) - 하루 종일 부분이 아닌 몇 분을 곱하십시오. * 24, 15- 소수점 이하 수 ) work_day_hours_diff로 your_table;

설명
SELECT task,
       start_time,
       end_time,
       ROUND(
         (
           -- Calculate the full weeks difference from the start of ISO weeks.
           ( TRUNC( end_time, 'IW' ) - TRUNC( start_time, 'IW' ) ) * (10/24) * (6/7)
           -- Add the full days for the final week.
             LEAST( TRUNC( end_time ) - TRUNC( end_time, 'IW' ), 6 ) * (10/24)
           -- Subtract the full days from the days of the week before the start date.
           - LEAST( TRUNC( start_time ) - TRUNC( start_time, 'IW' ), 6 ) * (10/24)
           -- Add the hours of the final day
             LEAST( GREATEST( end_time - TRUNC( end_time ) - 8/24, 0 ), 10/24 )
           -- Subtract the hours of the day before the range starts.
           - LEAST( GREATEST( start_time - TRUNC( start_time ) - 8/24, 0 ), 10/24 )
         )
         -- Multiply to give minutes rather than fractions of full days.
         * 24,
         15 -- Number of decimal places
       ) AS work_day_hours_diff
FROM   your_table;

  • 쿼리는 먼저 trunc (end_time, 'iw'iw를 사용하여 범위 내에서 전체 몇 주를 계산합니다. ') 및 trunc (start_time,'iw '). 이 계산은 작업의 시작과 끝을 포함하는 ISO 주간의 시작 및 종료 날짜를 제공합니다.
  • days : 최소한 () 및 가장 큰 () 함수는 부분을 설명하는 데 사용됩니다. 업무 시간이 적용되지 않는 범위의 시작과 끝에서의 날. 제공된 샘플 데이터와 함께 :
  • 작업 | start_time | end_time A | 16-Jan-17 10:00 | 23-Jan-17 11:35 B | 18-Jan-17 17:53 | 19-Jan-17 08:00 C | 13-Jan-17 13:00 | 17-17 14:52 d | 21-Jan-17 10:00 | 30-Jan-17 08:52 쿼리는 다음과 같은 결과를 출력합니다. end_time
  • work_day_hours_diff

2017-01-16 10:00:00 (mon)

&&] 2017-1-23 11:35:00 (월) 00 (wed)
SELECT task,
       start_time,
       end_time,
       ROUND(
         (
           -- Calculate the full weeks difference from the start of ISO weeks.
           ( TRUNC( end_time, 'IW' ) - TRUNC( start_time, 'IW' ) ) * (10/24) * (6/7)
           -- Add the full days for the final week.
             LEAST( TRUNC( end_time ) - TRUNC( end_time, 'IW' ), 6 ) * (10/24)
           -- Subtract the full days from the days of the week before the start date.
           - LEAST( TRUNC( start_time ) - TRUNC( start_time, 'IW' ), 6 ) * (10/24)
           -- Add the hours of the final day
             LEAST( GREATEST( end_time - TRUNC( end_time ) - 8/24, 0 ), 10/24 )
           -- Subtract the hours of the day before the range starts.
           - LEAST( GREATEST( start_time - TRUNC( start_time ) - 8/24, 0 ), 10/24 )
         )
         -- Multiply to give minutes rather than fractions of full days.
         * 24,
         15 -- Number of decimal places
       ) AS work_day_hours_diff
FROM   your_table;

2017-01-19 08:00:00 (thu)

. 201즈 ] D 2017-01-30 08:52:00 (mon) 68.8666666666666667 이 결과는 지정된 영업 시간을 기준으로 근무 시간 수를 정확하게 반영합니다.
2017-01-21 10:00:00 (sat)
최신 튜토리얼 더>

부인 성명: 제공된 모든 리소스는 부분적으로 인터넷에서 가져온 것입니다. 귀하의 저작권이나 기타 권리 및 이익이 침해된 경우 자세한 이유를 설명하고 저작권 또는 권리 및 이익에 대한 증거를 제공한 후 이메일([email protected])로 보내주십시오. 최대한 빨리 처리해 드리겠습니다.

Copyright© 2022 湘ICP备2022001581号-3