I have data with a subset that looks something like this:
create table tbl_1 as (
select * from (
select trunc(sysdate - (rownum - 1)) as call_dt,
rownum as calls,
to_char(trunc(sysdate - (rownum - 1)), 'DAY') as dow
from dual connect by rownum <= 22
)
where dow like '%MONDAY%'
order by call_dt
)
;
call_dt | calls | dow
-------------------------
17-SEP-18 22 MONDAY
24-SEP-18 15 MONDAY
01-OCT-18 8 MONDAY
08-OCT-18 1 MONDAY
I then have another table with future dates that looks like this:
create table tbl_2 as (
select * from (
select trunc(sysdate + (rownum - 1)) as call_dt,
0 as calls,
to_char(trunc(sysdate + (rownum - 1)), 'DAY') as dow
from dual
connect by rownum <= 15
)
where dow like '%MONDAY%'
)
;
call_dt | calls | dow
-------------------------
15-OCT-18 0 MONDAY
22-OCT-18 0 MONDAY
I am trying to take my future dates, append them to my historical data, and then calculate a rolling weighted average. I am currently doing this with the following query.
select call_dt,
case when calls = 0 then (
(1 * lag1) + (0.8 * lag2) + (0.5 * lag3) + (0.3 * lag4))
else calls
end as calls,
dow
from (
select call_dt, calls, dow,
lag(calls, 4) OVER (partition by dow order by call_dt) as lag4,
lag(calls, 3) OVER (partition by dow order by call_dt) as lag3,
lag(calls, 2) OVER (partition by dow order by call_dt) as lag2,
lag(calls, 1) OVER (partition by dow order by call_dt) as lag1
from (
select * from tbl_1
union
select * from tbl_2
)
order by dow, call_dt
)
;
This results in the following:
call_dt | calls | dow
-------------------------
17-SEP-18 22 MONDAY
24-SEP-18 15 MONDAY
01-OCT-18 8 MONDAY
08-OCT-18 1 MONDAY
15-OCT-18 46 MONDAY
22-OCT-18 24 MONDAY
This works great for the first future date for each day of week. However, for subsequent dates, the lag*
variables are equal to 0, and so the value is off. Here is what I'm hoping to achieve:
call_dt | calls | dow
-------------------------
17-SEP-18 22 MONDAY
24-SEP-18 15 MONDAY
01-OCT-18 8 MONDAY
08-OCT-18 1 MONDAY
15-OCT-18 46 MONDAY
22-OCT-18 70 MONDAY
I looked at this question, which seems like it might give me what I want? But the window function keywords used were foreign to me. I also looked at this tutorial, but it seems like these rolling average functions assume non-zero entries. Is it possible to achieve these results?
RANGE BETWEEN INTERVAL '4' DAY PRECEDING AND INTERVAL '4' DAY FOLLOWING
– Wernfried DomscheitOracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production
– tblznbitsBETWEEN INTERVAL '4' DAY PRECEDING AND INTERVAL '4' DAY FOLLOWING
works? Does that create a +/- 4 day window around the date of the current row Oracle is operating on? – tblznbits