在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 OutputSELECT 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_time2017-01-13 13:00:00 (FRI)
2017-01-17 14:52:00 (TUE)31.866666666666667SELECT 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
免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。
Copyright© 2022 湘ICP备2022001581号-3