So I am trying to insert aggregated (summed) values into the value table by hour. After I insert the values by hour, I would like to insert the comprehensive sum by day within the same procedure. Is there a way I can have the stored procedure complete the insert by hour and then have it draw a cursor by summing the data just inserted during the 'hourly' process? Would I need to commit the first transaction and then create another cursor shortly after?
Below is some sample data:
Result of query that is already summing / aggregating by hour:
SUM (VALUE_TX) UTC_OFFSET DATA_DATE HR
5 8 902018 1
10 8 902018 2
4 8 902018 3
What I want inserted after the first result set above is inserted into the table. I will be drawing these values from the same table that I'll be inserting on.
SUM (VALUE_TX) UTC_OFFSET DATA_DATE HR
19 8 902018 null
I was thinking of doing the following but I am not sure if this is the best way to do it. I was going to just group by data_Date to form the second cursor.
Create or replace procedure AGG_HOURLY_DAILY IS
CURSOR c1 is
Select sum(value_Tx) as sum_of_values
, utc_offset
, data_date
, hr
from value
group by data_date, utc_offset, hr
where HR is not null;
l_var c1%ROWTYPE;
CURSOR c2 is
Select sum(value_tx)
, utc_offset
, data_date
, hr
group by data_date, utc_offset, hr
Where HR is null;
k_var c2%rowtype;
BEGIN
Open C1;
LOOP;
FETCH c1 into l_var;
EXIT WHEN c1%NOTFOUND;
Insert into value(value_id, value_tx, utc_offset, data_date, hr)
values(null, l_var.sum_of_values, l_var.utc_offset, l_var.data_date, l_var.hr);
END LOOP;
CLOSE C1;
OPEN C2;
LOOP;
FETCH c2 into k_var;
EXIT WHEN c2%NOTFOUND;
Insert into value(value_id, value_tx, utc_offset, data_date, hr)
values(null, l_var.sum_of_values, l_var.utc_offset, l_var.data_date, l_var.hr);
END LOOP;
Close c1;
Close c2;
END AGG_HOURLY_DAILY;
NOTE: Value_ID is auto-generated by a sequence/trigger that I've created