Calculation of hours - weekends already excluded and how to exclude a vacation?

CREATE OR REPLACE FUNCTIONworking_hours
(in_start_dtINDATEDEFAULT SYSDATE
,in_end_dtINDATEDEFAULTSYSDATE

)

RETURNNUMBER

DETERMINISTIC

IS

--*************************************
--****
--* b u s i n e s s __ h o u s *.
--****
--*************************************

--working_hours returns the number of work houts (08:00-20:00)
--Monday to Friday) between in_start_dt and in_end_dt.
--If in_start_dt > in_end_dt, the results will be < = 0.
--Holidays are not considered.
dNUMBER;-Start_dt hours or end_dt after midnight
end_dtDATE: = GREATEST (in_start_dt, in_end_dt);  -In the case of dates were in disarray
return_valNUMBER;-The number of hours of work
start_dtDATE: = LEAST(in_start_dt, in_end_dt);  -In the case of dates were in disarray

holiday_cnt NUMBER;

BEGIN

WITHall_daysAS
(
SELECTstart_dt + LEVEL - 1AS a_dt
OfDouble
CONNECTION OF LEVEL < = 1 + TRUNC (end_dt) - TRUNC (start_dt)
LESS
SELECT trunc (calendar_day)
Ofpublic_holiday
)
SELECTSUM (12)
INreturn_val
Ofall_days
WHERETo_char (a_dt
, "Dy".
, ' NLS_DATE_LANGUAGE = "ENGLISH"'
)NOT IN ('Friday', 'Saturday');
-Adjust the hours of start_dt, if necessary
IF TO_CHAR (start_dt
, "Dy".
, ' NLS_DATE_LANGUAGE = "ENGLISH"'
) NOT IN ('Friday', 'Saturday')

THEN

SELECT COUNT (*)
INholiday_cnt
Ofpublic_holiday
WHERETRUNC (Calendar_Day) between TRUNC (start_dt) and TRUNC (end_dt);
IF holiday_cnt > 0
THEN
d: = 24 * (start_dt - TRUNC (start_dt));
IF d > = 20
THEN-Do not count start_dt himself
return_val: = return_val - 12;
ELSIF > 8 d
THEN-Do not count part of the start_dt
return_val: = return_val - (d - 8);
END IF;
END IF;

END IF;

-Adjust the hours of end_dt, if necessary
IF TO_CHAR (end_dt
, "Dy".
, ' NLS_DATE_LANGUAGE = "ENGLISH"'
) NOT IN ('Friday', 'Saturday')
THEN
d: = 24 * (end_dt - TRUNC (end_dt));
IF d < = 8
THEN-Do not count end_dt himself
return_val: = return_val - 12;
ELSIF d < 20
THEN-Do not count part of the end_dt
return_val: = return_val - (20 - d);
END IF;
END IF;

IF in_start_dt > in_end_dt
THEN
return_val: = - return_val;
END IF;

RETURNreturn_val;
ENDworking_hours

;

Select holiday h_day

H_DAY
30/12/2015
28/10/2015
25/08/2015
08/12/2015

create or replace function business_hours (p_start_timestamp in p_end_timestamp in timestamp, timestamp)

Return number

is

number of l_start_bh: = 8;

number of l_end_bh: = 20;

number of result;

Start

Select business_hrs

as a result

of (with

business_hours (start_dt, end_dt, daily_bh, Step) as

(select least (dr.start_dt, dr.end_dt), (dr.start_dt, dr.end_dt), greatest)

-case when to_char (least (dr.start_dt, dr.end_dt), 'dy') in ("Fri", "sat")

then-1

When the h.h_day is not null

then-2

on the other case when least (greatest (dr.start_dt, dr.end_dt),

trunc (least (Dr.start_dt, Dr.end_dt)) + l_end_bh / 24

) -

Greatest (least(Dr.start_dt,Dr.end_dt), trunc (least (Dr.start_dt, Dr.end_dt)) + l_start_bh / 24) > = 0

then least (greatest (dr.start_dt, dr.end_dt),

trunc (least (Dr.start_dt, Dr.end_dt)) + l_end_bh / 24

) -

Greatest (least(Dr.start_dt,Dr.end_dt), trunc (least (Dr.start_dt, Dr.end_dt)) + l_start_bh / 24)

0 otherwise

end

end daily_bh

1

from (select cast (p_start_timestamp as date) start_dt,)

Cast (p_end_timestamp as date) end_dt

of the double

) dr

left outer join

Holiday:

We h.h_day = trunc (least (start_dt, end_dt))

Union of all the

Select start_dt, end_dt,.

-case when to_char (trunc (start_dt) + No, "dy") in ("Fri", "sat")

then-1

When (select h_day vacation where h_day = trunc (start_dt) + not) is not null

then-2

on the other case when trunc (start_dt) + no<>

then (l_end_bh - l_start_bh) / 24

to another least (end_dt, trunc (end_dt) + l_end_bh / 24)-(trunc (end_dt) + l_start_bh / 24)

end

end,

Step + 1

of business_hours

where not<= trunc(end_dt)="" -="">

)

Select start_dt to_char (min (start_dt), 'dd.mm.yyyy hh24:mi:ss'),

end_dt to_char (max (end_dt), 'dd.mm.yyyy hh24:mi:ss'),

Sum (ceil (Greatest(0,daily_bh) / (1 / 24 / 60))) business_hrs

of business_hours

);

return the result;

end;

Select business_hours (to_timestamp ('10:20:30.123456 2015-12-01', 'yyyy-mm-dd hh24:mi:ss.ff'),)

TO_TIMESTAMP ('16:40:50.654321 2015-12-31', 'yyyy-mm-dd hh24:mi:ss.ff')

) minutes

of the double

MINUTES
14781

Concerning

Etbin

Tags: Database

Similar Questions

Maybe you are looking for

  • Program of applications Applications compatibility programs

    Incompatible program after installation of Intuit Canada software is tax quick 2012. Message received that there is no compatibility mode Windows with this new software. What do you suggest me?

  • HP 250 G2: Coffee on laptop HP 250 G2

    Hi a paid staff member a cup of coffee on his HP 250 Portable G2 screen has been damaged so I ordered a replacement screen and told him to back up his files just incase the screen isn't the only damage. I replaced the screen with a new job one I turn

  • I feel stupid asking this (microSD question)

    But how do I actually put files on my microSD card and NOT on the internal memory of the rocket itself? I'm sure it's something incredibly simple and I feel like a fool for asking this, but I have looked through the user guides, many other sources an

  • Should I suspend for instant backup?

    I have guests Windows 2008 R2, which are mainly used for the performance of work of statistical data and utilities such as printing, SFTP and application sharing. There are 2 running SQL, but not Exchange, Sharepoint, etc. Also some Linux Apache web

  • Remove the device detached using powershell scripts.

    $E01 = get-esxcli - vmhost test01$ED01 = $e01.storage.core.device.detached.list () # I have 10 devices here$e01.storage.core.device.detached.remove ($ED01) error as followsPowerCLI C:\scripts > $e01.storage.core.device.detached.remove ($ed01)A specif