Skip to content
Advertisement

SQLite – Rolling Average/Sum

I have a dataset as shown below, wondering how I can do a rolling average with its current record followed by next two records. Example: lets consider the first record whose total is 3 followed by 4 and 7 ,Now the rolling 3 day average for first record would be 4.6 and so on.

Expected output:

PS: Having “null” value isn’t important. This is just a sample data where I need to look at more than 3 days(Ex: 30 days rolling)

Advertisement

Answer

I think that the simplest approach is a window avg(), with the poper window frame:

If you want to return a null value when there is less than 2 leading rows, as show in your expected results, then you can use row_number() on top of it:

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement