get last 3 months sales data in mysql

How to Get Last 3 Months Sales Data in MySQL

It is useful to get previous 3 months sales data to understand sales trends, do reporting & data analysis. Here’s how to get last 3 months sales data in MySQL for your business/website. We will look at how to¬†select records for past 3 months using INTERVAL function in MySQL.

 

How to Get Last 3 Months Sales Data in MySQL

Here are the steps to get last 3 months sales data in MySQL. Let’s say you have the following table sales(order_date, amount) that contains daily sales information.

mysql> select order_date,sale from sales;

(showing only last few records)
+------------+------+
| order_date | sale |
+------------+------+
| 2020-01-28 |  230 |
|        ... |  ... |
| 2020-05-29 |  300 |
| 2020-05-30 |  250 |
| 2020-05-31 |  250 |
| 2020-06-01 |  250 |
| 2020-06-02 |  150 |
| 2020-06-03 |  300 |
| 2020-06-04 |  200 |
| 2020-06-05 |  200 |
| 2020-06-06 |  250 |
| 2020-06-07 |  150 |
| 2020-06-08 |  300 |
| 2020-06-09 |  200 |
+------------+------+

Bonus Read : How to Get Records of Current Month

 

Here’s the SQL query to get last 3 months sales data in MySQL, also known as rolling 3 months sales. We use INTERVAL()¬†function to get sales data for last 3 months.

mysql>select order_date,sale from sales
where order_date > now() - INTERVAL 3 MONTH;

(showing only last few records)
+------------+------+
| order_date | sale |
+------------+------+
| 2020-02-09 |  230 |
|        ... |  ... |
| 2020-05-29 |  300 |
| 2020-05-30 |  250 |
| 2020-05-31 |  250 |
| 2020-06-01 |  250 |
| 2020-06-02 |  150 |
| 2020-06-03 |  300 |
| 2020-06-04 |  200 |
| 2020-06-05 |  200 |
| 2020-06-06 |  250 |
| 2020-06-07 |  150 |
| 2020-06-08 |  300 |
| 2020-06-09 |  200 |
+------------+------+

In the above SQL query, we tell MySQL to get sales data for all dates where order_date is within our specified INTERVAL, that is, past 3 months from NOW.

 

Bonus Read : How to Create Histogram in MySQL

 

Since different months have different number of days, when you mention INTERVAL 3 months, MySQL will get data after exact same day of the month, before 3 months. That is, if today is June 9, it will get sales data from Mar 9.

If you want to get last 90 days sales data, then here’s the SQL query to get sales data from last 90 days.

mysql>select order_date,sale from sales
where order_date > now() - INTERVAL 90 DAY;

(showing only last few records)
+------------+------+
| order_date | sale |
+------------+------+
| 2020-03-11 |  230 |
|        ... |  ... |
| 2020-05-29 |  300 |
| 2020-05-30 |  250 |
| 2020-05-31 |  250 |
| 2020-06-01 |  250 |
| 2020-06-02 |  150 |
| 2020-06-03 |  300 |
| 2020-06-04 |  200 |
| 2020-06-05 |  200 |
| 2020-06-06 |  250 |
| 2020-06-07 |  150 |
| 2020-06-08 |  300 |
| 2020-06-09 |  200 |
+------------+------+

 

Bonus Read : How to Calculate Conversion Rate in MySQL

 

If you have multiple rows for each order_date, then you will need to aggregate daily sales when you get last 3 months sales data. For example, if you have sales table, as shown below

mysql> select order_date,sale from sales;

(showing only last few records)
+---------------------+------+
| order_date          | sale |
+---------------------+------+
| 2020-01-28 09:30:35 |  23  |
| 2020-01-28 10:10:00 |  30  |
| 2020-01-28 11:00:15 |  20  |
| 2020-01-28 14:50:35 |  15  |
| 2020-01-28 15:30:36 |  25  |
| 2020-01-28 17:10:55 |  15  |
|                 ... |  ... |
+---------------------+------+

Then here’s the query to get last 3 months sales data, after aggregating daily sales

mysql>select date(order_date),sale from sales
      where order_date > now() - INTERVAL 3 MONTH
      group by date(order_date);

(showing only last few records)
+------------+------+
| order_date | sale |
+------------+------+
| 2020-03-09 |  230 |
|        ... |  ... |
| 2020-05-29 |  300 |
| 2020-05-30 |  250 |
| 2020-05-31 |  250 |
| 2020-06-01 |  250 |
| 2020-06-02 |  150 |
| 2020-06-03 |  300 |
| 2020-06-04 |  200 |
| 2020-06-05 |  200 |
| 2020-06-06 |  250 |
| 2020-06-07 |  150 |
| 2020-06-08 |  300 |
| 2020-06-09 |  200 |
+------------+------+

You can plot this sales data on a line chart using a charting tool such as Ubiq.

daily sales data for last 3 months

 

You can also aggregate this sales data for each month using the following query. We use DATE_FORMAT to get month names from date values.

 

mysql>select date_format(order_date,'%b'),sale from sales
      where order_date > now() - INTERVAL 3 MONTH
      group by date_format(order_date,'%b');


+------------+-------+
| order_date |  sale |
+------------+-------+
| Mar        |   830 |
| Apr        | 10300 |
| May        | 12250 |
| Jun        |  1250 |
+------------+-------+

and plot it on a bar chart as shown below, created using Ubiq.

monthly sales last 3 months

 

That’s it! Hopefully, you too can get last 3 months sales data in MySQL for your business/website/app and share it with your team.

If you want to create charts, dashboards & reports from MySQL database, you can try Ubiq. We offer a 14-day free trial.

mm

About Ubiq

Ubiq is a powerful dashboard & reporting platform. Build dashboards, charts & reports for your business in minutes. Try it for free!