編寫一個SQL查詢,該查詢應返回之前91天窗口的平均費用。我將截止日期截至一週的第一天,並預計到今天爲止的91天平均值爲91天前前。Oracle Analytic Preceding返回錯誤/未預料的結果
但是,在驗證它時,似乎91天的平均值包含了我截斷的當前星期。
查詢:
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
電流輸出:
如果您有類似於我上面的問題,我強烈建議您查看sheetchqueez頻道的YouTube分析功能概述,http://www.youtube.com/user/sheepsqueezersYT/search ?query = analytic或http://www.sheepsqueezers.com/。 – Cimplicity 2012-02-09 14:40:08