Writing a SQL query that should return the average charges for a preceding 91 day window. I truncate the date down to the first day of the week and expect the 91 day average to be the 91 days prior to this date.
However when validating it appears that the 91 day average is including the current week that I truncated.
Query:
SELECT
week AS WEEK,
to_char(week,'ww-yyyy') AS Week_num
--Sum of the charges for the previous 91 days / 91 will give you avg charges per day for the last 91 days
--Need to count days regardless of if there are any charges on that day
,(SUM(CHARGES) OVER (ORDER BY WEEK RANGE INTERVAL '91' DAY PRECEDING))/91 MV_91_DAY_AVG
FROM
(
SELECT
--Truncate date down to first of week. Goal is to make 91 day preceding being at this date.
TRUNC(TRANS.ORIG_POST_DATE,'WW') AS WEEK
,SUM(TRANS.AMOUNT) AS CHARGES
FROM TRANS
WHERE
TRANS.DETAIL = "Charge"
GROUP BY TRUNC(TRANS.ORIG_POST_DATE,'WW')
)
ORDER BY WEEK
Current output:

I think you want your window clause to be:
in order to exclude the current week’s charges.
You may also want to change the ’91’ to ’92’ to include an additional week at the beginning.