」工欲善其事,必先利其器。「—孔子《論語.錄靈公》
首頁 > 程式設計 > 如何計算Oracle SQL的兩個日期之間的營業時間?

如何計算Oracle SQL的兩個日期之間的營業時間?

發佈於2025-02-06
瀏覽:477

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

工作時間規範 the Usistion The Us The Bucky Tours是星期一至週六的8:00至下午6:00。要將這些小時納入計算中,您可以使用日期操縱和有條件的邏輯的組合。

選擇任務, 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) - 減去範圍開始前一天的時間。 - 最少(最大(start_time -trunc(start_time)-8/24,0),10/24) ) - 乘以幾分鐘而不是整天的分數。 * 24, 15-小數點數量 )作為work_day_hours_diff 來自your_table;

days: diste the the diste the the the distect andest()和最大()函數用於部分解釋部分在範圍的開始和結束時,營業時間可能無法應用。 小時:

的最後一天和範圍啟動前一天的差異相似。

Multiplication: The final calculation multiplies the result by 24 to convert minutes to hours.

Sample Data and Output
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;

帶有提供的示例數據:任務| start_time | end_time A | 17年1月16日10:00 | 17年1月23日11:35 b | 17年1月18日17:53 | 17年1月19日08:00 C | 17年1月13日13:00 | 17年1月17日14:52 D | 17年1月21日10:00 | 30-JAN-17 08:52 查詢輸出以下結果:

end_time
    work_day_hours_diff
  • 2017-01-23 11:35:00 (MON)61.583333333333333
  • B
  • 2017-01-18 17:53: 00 (WED)2017-01-19 08 :00:00 (THU)
  • .116666666666667
C

2017-01-13 13:00:00 (FRI)

2017-01-17 14:52:00 (TUE)

31.866666666666667
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;

d 2017-01-30 08:52:00(mon) [&& && && && &&華
最新教學 更多>

免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。

Copyright© 2022 湘ICP备2022001581号-3