Scenario:
I want to calculate the moving average for sales amount for the entire month that has taken place SO FAR. Each month has a different number of days. If the sales_amount is 0, that means that person has the day off and I need to ignore that value.
Sample Data:
sales_amount date
100 2021-04-01
200 2021-04-02
300 2021-04-03
0 2021-04-04
100 2021-04-05
The Final moving_average value is (100 + 200 + 300 + 100) / (4) since we are ignoring April 4th because it has a 0 sales amount.
What I have found on this site that doesn't meet my needs: https://www.sqlservercentral.com/articles/calculate-moving-averages-using-t-sql-in-sql-server but it is for a certain number of days, how can I apply this when each month has a different number of days and I want to ignore certain rows?
Expected Result:
sales_amount date moving_average
100 2021-04-01 100
200 2021-04-02 150
300 2021-04-03 200
0 2021-04-04 200
100 2021-04-05 175
What I have tried doing:
AVG (Sales_Amount) OVER (
Partition BY [Date]
Order by [Date]
,Rows Between Unbounded Preceding and Current Row)