根据Oracle SQL中的营业时间计算营业时间

编程入门 行业动态 更新时间:2024-10-27 10:29:32
本文介绍了根据Oracle SQL中的营业时间计算营业时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我希望根据工作时间来计算任务开始和结束时间之间的时间.我有以下示例数据:

I am looking to calculate hours between a start and end of time of a task based on business hours. I have the following sample data:

TASK | 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-JAN-17 14:52 D | 21-JAN-17 10:00 | 30-JAN-17 08:52

我需要计算出两者之间的区别,但要基于以下工作时间:

and I need to work out the difference between the two but based on the following business hours:

Mon - Sat 08:00 - 18:00

我知道如何编写计算,但不确定如何将营业时间添加到计算中.

I know how to write the calculation but not sure what I do to add the business hours into the calculation.

任何建议将不胜感激.

推荐答案

使用相关的层次结构查询为每个工作日生成一行,然后对每一天的小时总数求和:

Use a correlated hierarchical query to generate one row for each work day and then sum the hours for each day:

SELECT task, COALESCE( SUM( end_time - start_time ), 0 ) * 24 AS total_hours FROM ( SELECT task, GREATEST( t.start_time, d.column_value + INTERVAL '8' HOUR ) AS start_time, LEAST( t.end_time, d.column_value + INTERVAL '18' HOUR ) AS end_time FROM your_table t LEFT OUTER JOIN TABLE( CAST( MULTISET( SELECT TRUNC( t.start_time + LEVEL - 1 ) FROM DUAL WHERE TRUNC( t.start_time + LEVEL - 1 ) - TRUNC( t.start_time + LEVEL - 1, 'iw' ) < 6 CONNECT BY TRUNC( t.start_time + LEVEL - 1 ) < t.end_time ) AS SYS.ODCIDATELIST ) ) d ON ( t.end_time > d.column_value + INTERVAL '8' HOUR AND t.start_time < d.column_value + INTERVAL '18' HOUR ) ) GROUP BY task;

更多推荐

根据Oracle SQL中的营业时间计算营业时间

本文发布于:2023-10-23 01:37:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1519353.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:营业时间   Oracle   SQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!