简体   繁体   中英

3 month rolling average with missing months

I've been reading the related questions here, and so far the solutions require that there are no missing months. Would love to get some help on what I can do if there are missing months?

For example, I'd like to calculate the 3 month rolling average of orders per item. If there is a missing month for an item, the calculation assumes that the number of orders for that item for that month is 0. If there are fewer than three months left, the rolling average isn't so important (it can be null or otherwise).

MONTH   | ITEM | ORDERS | ROLLING_AVG
2021-04 | A    | 5      |  3.33
2021-04 | B    | 4      |  3
2021-03 | A    | 3      |  1.66
2021-03 | B    | 5      |  null
2021-02 | A    | 2      |  null
2021-01 | B    | 2      |  null

Big thanks in advance!

Also, is there a way to "add" the missing month rows without using a cross join with a list of items? For example if I have 10 million items, the cross join takes quite a while to execute.

You can use a range window frame -- and some conditional logic:

select t.*,
       (case when min(month) over (partition by item) <= month - interval '2 month'
             then sum(orders) over (partition by item
                                    order by month
                                    range between interval '2 month' preceding and current row
                                   ) / 3.0
        end) as rolling_average
from t;

Here is a db<>fiddle. The results are slightly different from what is in your question, because there is not enough info for A in 2021-03 but there is enough for B in 2021-03.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM