Compute a Moving Average in MySQL
Last Updated :
30 Apr, 2024
Moving the averages is an important technique in the field of data analysis and time-series data processing. By reducing data fluctuations moving averages aid in highlighting trends in the data.
We'll examine how to compute moving averages in MySQL a well-liked relational database administration system in this post. Gaining an understanding of this concept can improve your capacity to get useful information from the data.
Moving Averages
Let's review moving averages in brief before getting into implementation. By calculating a series of averages for the various subsets of the entire dataset, one can examine data points using the moving average technique. By smoothing out the data this is done to find trends or patterns.
Computing a Simple Moving Average
The simplest form of the moving average is the simple moving average which calculates the mean of the fixed number of the data points over a specified period. Let's illustrate this with an example of computing a 5-day SMA for the daily stock prices.
Creating Sample Data
To demonstrate computing moving averages in the MySQL let's first create some sample data. We'll create a simple table with the two columns: date and value representing the date and corresponding numeric value.
CREATE TABLE sample_data (
date DATE,
value DECIMAL(10, 2)
);
INSERT INTO sample_data (date, value)
VALUES
('2024-01-01', 10.5),
('2024-01-02', 12.3),
('2024-01-03', 11.8),
('2024-01-04', 14.2),
('2024-01-05', 13.5),
('2024-01-06', 12.6),
('2024-01-07', 13.8),
('2024-01-08', 15.1),
('2024-01-09', 16.5),
('2024-01-10', 17.2);
Output:
date | value |
---|
2024-01-01 | 10.5 |
2024-01-02 | 12.3 |
2024-01-03 | 11.8 |
2024-01-04 | 14.2 |
2024-01-05 | 13.5 |
2024-01-06 | 12.6 |
2024-01-07 | 13.8 |
2024-01-08 | 15.1 |
2024-01-09 | 16.5 |
2024-01-10 | 17.2 |
Computing Moving Average
Now, let's compute the moving average using the MySQL. We'll use a window function to calculate the average of the last n rows where n is the window size.
SELECT
date,
value,
(SELECT AVG(value)
FROM sample_data AS s2
WHERE s2.date BETWEEN DATE_SUB(s1.date, INTERVAL 2 DAY) AND s1.date) AS moving_avg
FROM
sample_data AS s1;
Output:
date | value | moving_avg |
---|
2024-01-01 | 10.50 | 10.50000 |
2024-01-02 | 12.30 | 11.40000 |
2024-01-03 | 11.80 | 11.53333 |
2024-01-04 | 14.20 | 12.76667 |
2024-01-05 | 13.50 | 13.16666 |
2024-01-06 | 12.60 | 13.43333 |
2024-01-07 | 13.80 | 13.30000 |
2024-01-08 | 15.10 | 13.83333 |
2024-01-09 | 16.50 | 15.13333 |
2024-01-10 | 17.20 | 16.26667 |
Explanation: This query computes the moving average of the value column over a window of the 3 rows by using the correlated subquery. It calculates the average of the value column for the current row and the two preceding rows.
Example: Computing Running Totals for Monthly Expenses
Let's consider a similar scenario but with the monthly expenses data. Suppose we have a table named expenses with the following data:
Date
| Amount
|
---|
2024-04-01
|
50.00
|
2024-04-05
|
75.25
|
2024-04-10
|
120.25
|
2024-04-15
|
90.30
|
2024-04-20
|
110.60
|
We want to compute the running total of the expenses for each day.
SELECT
date,
amount,
@running_total := @running_total + amount AS running_total
FROM
expenses,
(SELECT @running_total := 0) AS rt
ORDER BY
date;
Output:
date | amount | running_total |
---|
2024-04-01 | 50.00 | 50.00 |
2024-04-01 | 50.00 | 100.00 |
2024-04-05 | 75.25 | 175.25 |
2024-04-05 | 75.25 | 250.50 |
2024-04-10 | 120.75 | 371.25 |
2024-04-10 | 120.75 | 492.00 |
2024-04-15 | 90.30 | 582.30 |
2024-04-15 | 90.30 | 672.60 |
2024-04-20 | 110.60 | 783.20 |
2024-04-20 | 110.60 | 893.80 |
Explanation:
- The amount column represents daily expenses.
- The running_total column displays the cumulative sum of the expenses up to each day.
- The @running_total variable is initialized to 0 and it accumulates the running total as the rows are processed.
Example: Computing Running Totals for Daily Sales
Suppose we have a table named sales with the following data:
Date
| Amount
|
---|
2024-04-01
|
100.50
|
2024-04-02
|
150.25
|
2024-04-03
|
200.75
|
2024-04-04
|
175.30
|
2024-04-05
|
225.60
|
We want to compute the running total of sales for the each day.
SELECT
date,
amount,
@running_total := @running_total + amount AS running_total
FROM
sales,
(SELECT @running_total := 0) AS rt
ORDER BY
date;
Output:
date | amount | running_total |
---|
2024-04-01 | 100.50 | 100.50 |
2024-04-02 | 150.25 | 250.75 |
2024-04-03 | 200.75 | 451.50 |
2024-04-04 | 175.30 | 626.80 |
2024-04-05 | 225.60 | 852.40 |
Explanation:
- The amount column represents daily sales figures.
- The running_total column displays the cumulative sum of the sales up to each day.
- The @running_total variable is initialized to 0 and it accumulates the running total as the rows are processed.
Conclusion
In this article, we've explored how to compute moving averages in MySQL using the window functions. By applying moving averages to the data we can gain insights into the trends and patterns which can be valuable for various analytical tasks. Experiment with the different window sizes and datasets to see how moving averages can help better understand your data.
Similar Reads
Compute Moving Average in PostgreSQL
PostgreSQL is an advanced relational database system that supports both relational (SQL) and non-relational (JSON) queries. It is free and open-source. The moving average helps to level the price data over a specified period by creating a constantly updated average price. PostgreSQL which, is an imp
6 min read
How to Compute a Moving Average in PL/SQL?
A moving average is a technique we use to analyze and determine some specific trends in our provided data. Through moving averages, we can analyze and determine trends in our data along with some other benefits like noise reduction in our data. In this article, we are going to learn about "how to co
5 min read
Calculate Moving Averages in SQL
In data analysis, smoothing out short-term fluctuations in time-series data is essential for identifying long-term trends. One effective method for achieving this is through the moving average, a widely used technique in business analytics, finance and forecasting. SQL provides several ways to compu
4 min read
Moving Averages in R
Moving averages (MA) play a crucial role in time series analysis, providing a means to discern trends, patterns, and underlying structures within sequential data. In the context of R, a popular programming language for statistical computing, various functions and packages facilitate the computation
6 min read
How to Calculate Moving Averages in Python?
In this discussion we are going to see how to Calculate Moving Averages in Python in this discussion we will write a proper explanation What is Moving Averages?Moving Averages, a statistical method in data analysis, smooths fluctuations in time-series data to reveal underlying trends. Calculating th
11 min read
Calculate Median in MySQL
The median is a statistical measure that represents the middle value in a sorted list of numbers. It separates the higher half from the lower half of a dataset. In SQL, calculating the median involves determining the middle value or the average of the two middle values in a sorted dataset. In this a
4 min read
How to calculate MOVING AVERAGE in a Pandas DataFrame?
Calculating the moving average in a Pandas DataFrame is used for smoothing time series data and identifying trends. The moving average, also known as the rolling mean, helps reduce noise and highlight significant patterns by averaging data points over a specific window. In Pandas, this can be achiev
7 min read
ADDDATE() function in MySQL
The ADDDATE() function in MySQL is a powerful tool for adding specific time intervals to date or datetime values. It simplifies data manipulation by allowing us to easily calculate future or adjusted dates based on a given starting point. In this article, we will learn about the ADDDATE() function i
3 min read
How to Calculate an Exponential Moving Average in Python?
Moving Averages are financial indicators which are used to analyze stock values over a long period of time. i.e. Average value for that long period is calculated. Exponential Moving Averages (EMA) is a type of Moving Averages. It helps users to filter noise and produce a smooth curve. In Moving Aver
3 min read
AVG() Function in SQL
SQL is an RDBMS system in which SQL functions become very essential to provide us with primary data insights. One of the most important functions is called AVG() and is particularly useful for the calculation of averages within datasets. In this, we will learn about the AVG() function, and its synta
4 min read