SQL moving average
时间: 2023-08-22 13:10:29 浏览: 118
data_avg.rar_SFM_moving average
SQL moving average refers to calculating the average of a specific column's values over a specified number of periods. Here is an example of how you can calculate a moving average in SQL:
```sql
SELECT date_column, value_column,
AVG(value_column) OVER (ORDER BY date_column ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) AS moving_average
FROM your_table;
```
In this example, `date_column` represents the column with the dates or timestamps, `value_column` represents the column for which you want to calculate the moving average, and `your_table` is the name of the table containing the data.
The `AVG` function is used with the `OVER` clause to calculate the moving average. The `ROWS BETWEEN 2 PRECEDING AND CURRENT ROW` specifies that the average should be calculated including the current row and the two preceding rows.
You can adjust the number of preceding rows as per your requirement.
阅读全文