🙏 We need your help! SQLtest.online has no ads and lives only on donations. Even a small amount helps. Donate now →
SQL code copied to buffer
Task 10:

Running Total and Moving Average of Orders

SQL Server 2022
For a sales chart, go through the orders of SalesOrderHeader in the order of SalesOrderID. For every order show the revenue so far and the average of the last three orders.
Output the columns SalesOrderID, TotalDue, running_total (the sum of TotalDue of this and all previous orders) and moving_avg (the average TotalDue of this order and the two before it, rounded to 2 decimal places; the first two orders average over what there is), sorted by SalesOrderID.
Expected result Columns: 4 · Rows: 32
SalesOrderID TotalDue running_total moving_avg

Sample rows open after wrong checks: 0 of 2

Write your query below and click "Check it!". Tables are described in the right pane.

Tutorial

By checking a query, you agree that it is stored without your account details and may be included in anonymized datasets, including for AI training. Learn more

The query result will appear here. Ctrl+Enter - Run query