I have a table in MYSQL(version 5.7.33) which looks like shown below:
Date | SalesRep | Sale |
---|---|---|
2021-04-01 | Jack | 10 |
2021-04-02 | Jack | 8 |
2021-03-01 | Lisa | 10 |
2021-03-02 | Lisa | 14 |
2021-03-03 | Lisa | 21 |
2021-03-04 | Lisa | 7 |
2021-03-08 | Lisa | 10 |
2021-03-09 | Lisa | 20 |
2021-03-10 | Lisa | 15 |
I want the moving average of Sale column, but don't want that to be based on the dates since the dates have gap, instead I want it based on row numbers and grouped by SalesRep. So something like this:
Date | SalesRep | Sale | MoveAvg |
---|---|---|---|
2021-04-01 | Jack | 10 | 10 |
2021-04-02 | Jack | 8 | 9 |
2021-03-01 | Lisa | 10 | 10 |
2021-03-02 | Lisa | 14 | 12 |
2021-03-03 | Lisa | 21 | 15 |
2021-03-04 | Lisa | 7 | 13 |
2021-03-08 | Lisa | 10 | 12.4 |
2021-03-09 | Lisa | 20 | 13.6 |
2021-03-10 | Lisa | 15 | 13.8 |
So the moving average is for all the dates from start to finish for a particular sales rep and then it starts over for another sales rep and so on. Is this possible to do in MYSQL? Thank you in advance!