Moving Average
prices has one closing price per day. Return every day with ma3: the average of that day's close and the closes of the two rows before it, rounded to 2 decimals. The first day averages one price and the second day two. Earliest day first.
Example 1
prices
| day | close |
|---|---|
| 2026-01-02 | 10.0 |
| 2026-01-05 | 20.0 |
| 2026-01-06 | 60.0 |
| 2026-01-07 | 10.0 |
returns
| day | ma3 |
|---|---|
| 2026-01-02 | 10.0 |
| 2026-01-05 | 15.0 |
| 2026-01-06 | 30.0 |
| 2026-01-07 | 30.0 |
Constraints
- Up to 14 days, each day once
- No average is a close call for the rounding
Write it in your browser, against the clock. The model query, with a note on each piece of it, comes after.
More SQL problems
- Position Over TimeHard
- SessionsHard
- Three Days RunningHard
- Top Two Salaries per DepartmentHard
- Worst DrawdownHard
- Between Two PricesBeginner
- Both ConditionsBeginner
- Cheapest FirstBeginner