I'm trying to calculate running averages of past 4th month. So I need to get the 4th value of each month
month_date | Month 1 | Month 2 | Month 3| Month 4
---------------------------------------------
11 | 0 | 0 | 0 | 0
10 | 2 | 0 | 0 | 0
09 | 3 | 4 | 0 | 0
08 | 8 | 7 | 9 | 0
07 | 6 | 8 | 11 | 5
06 | 3 | 4 | 0 | 8
05 | 8 | 7 | 9 | 9
04 | 6 | 8 | 11 | 5
[Expected Output]
| Month 1 | Month 2 | Month 3| Month 4
----------------------------------------
| 6 | 4 | 9 | 5
What I tried to do
- I tried to rank excluding zeros like row_number over (order by month desc) - that didn't work
-I tried to to use NULLS LAST function that didn't work either because I need to order based on month not on each month
Please help