Oracle如何消除非工作时间
Posted
技术标签:
【中文标题】Oracle如何消除非工作时间【英文标题】:How to eliminate non-working hours in Oracle 【发布时间】:2013-06-15 06:37:43 【问题描述】:我有两列 DateTime 类型。第一个存储进程启动时的日期时间,另一个存储进程完成时的日期时间。我想计算完成工作所需的总工作时间。工作时间为上午 10 点至下午 7 点,周日休息。
以下是我记下的一些 sn-ps:
SELECT col1, col2, floor(((date2-date1)*24*60*60)/3600)
|| ' HOURS ' ||
floor((((date2-date1)*24*60*60) -
floor(((date2-date1)*24*60*60)/3600)*3600)/60)
|| ' MINUTES ' ||
round((((date2-date1)*24*60*60) -
Floor(((date2-date1)*24*60*60)/3600)*3600 -
(floor((((date2-date1)*24*60*60) -
floor(((date2-date1)*24*60*60)/3600)*3600)/60)*60) ))
|| ' SECS ' Time_Difference
From Table_Name;
还有,
Select To_Number(To_Char(date1, 'HH24')) || ':'
|| to_number(to_char(date1, 'MI')) || ':'||
to_number(to_char(date1, 'SS')) from Table_Name
请帮忙。
对于你提到的上述解决方案,结果就是这样!
START DATE DAY FINISH DATE DAY Date Diff Total Hours Work Hours
07-AUG-12 21:55:21 TUE 08-AUG-12 11:09:10 WED 0 13:13:49.0 13 13
13-NOV-12 15:45:25 TUE 14-NOV-12 10:41:42 WED 0 18:56:17.0 18 18
20-DEC-12 20:31:03 THU 21-DEC-12 11:03:36 FRI 0 14:32:33.0 14 14
14-MAR-13 20:39:00 THU 15-MAR-13 11:00:04 FRI 0 14:21:4.0 14 14
07-JUN-12 21:17:36 THU 08-JUN-12 11:02:23 FRI 0 13:44:47.0 13 13
18-SEP-12 20:48:27 TUE 19-SEP-12 11:07:35 WED 0 14:19:8.0 14 14
新场景:https://***.com/questions/17248741/how-to-get-business-hours-between-two-dates-in-oracle
【问题讨论】:
【参考方案1】:如果我理解正确,您想计算开始日期和结束日期之间的差异,不包括上午 10 点之前和晚上 7 点之后的时间。
这是示例查询和sql fiddle。
SELECT start_time,
finish_time,
interval_time,
EXTRACT (HOUR FROM interval_time), --extract the hours,mins and seconds from the interval
EXTRACT (MINUTE FROM interval_time),
EXTRACT (SECOND FROM interval_time)
FROM (SELECT start_time,
finish_time,
NUMTODSINTERVAL (
CASE
WHEN finish_time - TRUNC (finish_time) > (19 / 24) --if finish time is after 7pm
THEN
TRUNC (finish_time) + (19 / 24) --set it to 7pm
ELSE
finish_time --else set it to actual finish time
END
- CASE
WHEN start_time - TRUNC (start_time) < (10 / 24) --if start time is before 10 am
THEN
TRUNC (start_time) + (10 / 24) --set it to 10 am.
ELSE
start_time --else set it to the actual start time
END,
'day') --subtract the both and convert the resulting day to interval
interval_time
FROM timings);
我所做的是,
检查开始时间是否在上午 10 点之前,结束时间是否在晚上 7 点之后。如果是这样,请将时间设置为上午 10 点和下午 7 点。 然后减去日期并将得到的天数转换为间隔类型。 然后从 Interval 中提取小时、分钟和秒。注意:此查询假定两个日期都在同一天,并且都不在上午 10 点之前或晚上 7 点之后。
更新: 排除节假日,查询会变得复杂。我建议编写三个函数并在查询中使用这些函数。
第一个功能:
FUNCTION modify_start_time (p_in_dte DATE) RETURN DATE
----------------------------------
IF p_in_dte - TRUNC (p_in_dte) < (10 / 24)
THEN
RETURN TRUNC (p_in_dte) + (10 / 24);
ELSIF p_in_dte - TRUNC (p_in_dte) > (19 / 24)
THEN
RETURN TRUNC (p_in_dte) + 1 + (10 / 24);
ELSE
RETURN p_in_dte;
END IF;
如果开始时间在工作时间之外,请将开始时间修改为下一个最近的开始时间。
第二个功能:
FUNCTION modify_finish_time (p_in_dte DATE) RETURN DATE
----------------------------------
IF p_in_dte - TRUNC (p_in_dte) > (19 / 24)
THEN
RETURN TRUNC (p_in_dte) + (19 / 24);
ELSIF p_in_dte - TRUNC (p_in_dte) < (10 / 24)
THEN
RETURN TRUNC (p_in_dte) - 1 + (19 / 24);
ELSE
RETURN p_in_dte;
END IF;
如果完成时间在工作时间之外,请将其修改为之前最近的完成时间。
第三个功能:
FUNCTION get_days_to_exclude (p_in_start_date DATE,
p_in_finish_date DATE) RETURN NUMBER
--------------------------------------------------------
WITH cte --get all days between start and finish date
AS ( SELECT p_in_start_date + LEVEL - 1 dte
FROM DUAL
CONNECT BY LEVEL <= p_in_finish_date + 1 - p_in_starT_date)
SELECT COUNT (1) * 9 / 24 --mutiply the days with work hours in a day
INTO l_num_holidays
FROM cte
WHERE TO_CHAR (dte, 'dy') = 'sun' --find the count of sundays
OR dte IN --fins the count of holidays, assuming leaves are stored in separate table
(SELECT leave_date
FROM leaves
WHERE leave_date BETWEEN p_in_start_date
AND p_in_finish_date);
l_num_holidays :=
l_num_holidays + ( (p_in_finish_date - p_in_start_date) * (15 / 24)); --also, if the dates span more than a day find the non working hours.
RETURN l_num_holidays;
此函数在计算持续时间时查找要排除的天数。
所以,最终的查询应该是这样的,
SELECT start_time,
finish_time,
CASE
WHEN work_duration < 0 THEN NUMTODSINTERVAL (0, 'day')
ELSE NUMTODSINTERVAL (work_duration, 'day')
END
FROM (SELECT start_time, finish_time,
--modify_start_time (start_time), modify_finish_time (finish_time),
modify_finish_time (finish_time)
- modify_start_time (start_time)
- get_days_to_exclude (
TRUNC (modify_start_time (start_time)),
TRUNC (modify_finish_time (finish_time)))
work_duration
FROM timings);
如果持续时间小于 0,则忽略它,将其设置为 0。
【讨论】:
当星期天或假期介于两个日期之间时该怎么办。我们如何排除这些时间? 我已经编辑并上传了您要求的示例数据。是的,开始和结束日期可以跨越一天以上。例如,如果开始日期是星期六,结束日期是星期一,那么日期差会超过一天!【参考方案2】:您可以计算两个日期之间的小时差,并减少非工作时间乘以两天之间的天数。 考虑这个例子:
create table t1 (
start_hour date , finish_hour date);
insert into t1 values (
to_date('01/01/2013 12:00:00','dd/mm/yyyy hh24:mi:ss'),
to_date('01/01/2013 15:00:00','dd/mm/yyyy hh24:mi:ss'));
insert into t1 values (
to_date('01/01/2013 12:00:00','dd/mm/yyyy hh24:mi:ss'),
to_date('02/01/2013 15:00:00','dd/mm/yyyy hh24:mi:ss'));
insert into t1 values (
to_date('01/01/2013 18:00:00','dd/mm/yyyy hh24:mi:ss'),
to_date('03/01/2013 11:00:00','dd/mm/yyyy hh24:mi:ss'));
with x as (
select start_hour ,finish_hour ,
(finish_hour - start_hour) * 24 hour_diff ,
trunc(finish_hour) - trunc(start_hour) as day_diff
from t1
)
select start_hour , finish_hour ,
hour_diff - 15 * day_diff as working_hours
from x;
| START_HOUR | FINISH_HOUR | WORKING_HOURS |
-----------------------------------------------------------------------------------
| January, 01 2013 12:00:00+0000 | January, 01 2013 15:00:00+0000 | 3 |
| January, 01 2013 12:00:00+0000 | January, 02 2013 15:00:00+0000 | 12 |
| January, 01 2013 18:00:00+0000 | January, 03 2013 11:00:00+0000 | 11 |
【讨论】:
以上是关于Oracle如何消除非工作时间的主要内容,如果未能解决你的问题,请参考以下文章