使用Loop获取多个hh24:mi值的总和

时间:2017-07-19 10:00:58

标签: sql oracle

我想总结一下特定日期标准之间的总工作时间。我已经找回了每天的工作时间。现在我想迭代一系列日期并总结工作时间和分钟。

以下是我正在努力开发的方法:

create or replace FUNCTION GET_Working_hrs_Total (ecd in number,start_dt in DATE,end_dt in date,stdin IN VARCHAR2,stdout in VARCHAR2)
RETURN varchar2 AS
--
hrs   varchar2(1000);
temp_date date;
--
BEGIN

temp_date :=start_dt;

while temp_date <=end_dt
LOOP

select olphrm.get_std_hrs(ecd,temp_date,stdin,stdout)--It will return the working hours of date passed for example 8:30 i.e. 8 hours 30 minutes
into hrs from dual;--I want to get the count of Total working hours in this variable

temp_date :=temp_date+1;--switch to next day
END LOOP;

        return hrs;         

END;

如何以16:30的格式(即16小时30分钟)实现总工时和分钟?

2 个答案:

答案 0 :(得分:1)

@ JohnHc回答中的一些变化之后:

以下代码对我有用:

create or replace FUNCTION GET_Working_hrs_Total (ecd in number,start_dt in DATE,end_dt in date,stdin IN VARCHAR2,stdout in VARCHAR2)
RETURN varchar2 AS
--
v_hours number(3,0) := 0; -- We'll put hours in here
v_mins  number(6,0) := 0; -- An mins in here
hrs   varchar2(1000);
temp_date date;
--
BEGIN

temp_date :=start_dt;

while temp_date <=end_dt
LOOP

SELECT EXTRACT(hour FROM to_timestamp(olphrm.GET_Working_hrs(ecd,temp_date,stdin,stdout), 'hh24:mi') ) + v_hours
into v_hours from dual; -- Add new hours to old hours


SELECT EXTRACT(MINUTE FROM to_timestamp(olphrm.GET_Working_hrs(ecd,temp_date,stdin,stdout), 'hh24:mi') ) + v_mins  
into v_mins from dual;-- Add new mins to old mins


temp_date :=temp_date+1;

END LOOP;

v_hours := v_hours + floor(v_mins / 60); -- Add mins to hours

v_mins := mod(v_mins, 60); -- and ditch the hours from mins

hrs:= to_char(v_hours, '999') ||':'||to_char(v_mins,'99'); -- create the varchar to return

        return hrs;         

END;

答案 1 :(得分:0)

尝试:

    create or replace FUNCTION GET_Working_hrs_Total (ecd in number,start_dt in DATE,end_dt in date,stdin IN VARCHAR2,stdout in VARCHAR2)
RETURN varchar2 AS
--
v_hours number(3,0) := 0; -- We'll put hours in here
v_mins  number(6,0) := 0; -- An mins in here
hrs   varchar2(1000);
temp_date date;
--
BEGIN

temp_date :=start_dt;

while temp_date <=end_dt
LOOP


select t-_number(to_char(olphrm.get_std_hrs(ecd,temp_date,stdin,stdout), 'HH24'), '999') + v_hours
into v_hours
from dual; -- Add new hours to old hours



select to_number(to_char(olphrm.get_std_hrs(ecd,temp_date,stdin,stdout), 'MI'), '999') + v_mins into v_mins  from dual; -- Add new mins to old mins

temp_date :=temp_date+1;

END LOOP;

v_hours := v_hours + floor(v_mins / 60); -- Add mins to hours

v_mins := mod(v_mins, 60); -- and ditch the hours from mins

hrs:= to_char(v_hours, '999') ||':'||to_char(v_mins,'99'); -- create the varchar to return

        return hrs;         

END;