Group by Year, Month, or Day in MySql

Group by Year, Month, or Day in MySQL

How to group data by specific date interval using DATE_FORMAT(), EXTRACT() functions, and some helpers like YEAR(), MONTH(), DAY(), and DATE() in MySQL.

Introduction

Previously, I wrote an article on how to group records by year, month, and day in PostgreSQL — if you haven’t seen it yet, you can check it out here.

In this follow-up, we’ll explore how to achieve the same results in MySQL. Using a sample orders table, we’ll go through different ways to group data by year, month, and day using functions like DATE_FORMAT(), EXTRACT(), YEAR(), MONTH(), DAY(), and more.

Whether you’re building reports, dashboards, or just exploring your data, these techniques will help you slice it by time with ease.

Your Manager Requirements

Before digging deeper, let’s consider that you have the following orders table:

idpricecreatedAt
1100.002022-01-12 01:15:00
2120.002022-02-10 23:44:00
380.002023-01-09 08:14:00
4150.002023-07-09 20:50:00
550.002023-07-10 09:33:00
630.002023-07-10 15:23:00
790.002023-09-23 10:12:00

And your manager asks you three requirements:

  1. Calculate the total sales every year.
  2. Calculate the total sales every month in 2023.
  3. Calculate the total sales every day in July 2023.

Group by Year

To calculate the total sales per year:

  1. Using DATE_FORMAT()
SELECT DATE_FORMAT(createdAt, '%Y') AS year, SUM(price) AS total
FROM orders
GROUP BY year
ORDER BY year;

This statement returns:

yeartotal
2022220.00
2023400.00
  1. Using EXTRACT()
SELECT EXTRACT(YEAR FROM createdAt) AS year, SUM(price) AS total
FROM orders
GROUP BY year
ORDER BY year;

And it returns the same result.

  1. Using YEAR()
SELECT YEAR(createdAt) AS year, SUM(price) AS total
FROM orders
GROUP BY year
ORDER BY year;

Also, returns the same output.

Group by Month in a Year

To calculate the total sales per month in 2023:

  1. Using DATE_FORMAT()
SELECT DATE_FORMAT(createdAt, '%Y-%m') AS `year_month`, SUM(price) AS total
FROM orders
WHERE YEAR(createdAt) = 2023
GROUP BY `year_month`
ORDER BY `year_month`;

This query returns:

year_monthtotal
2023-0180.00
2023-07230.00
2023-0990.00

💌 Enjoying this post? Subscribe to get more practical dev tips right in your inbox.

  1. Using EXTRACT()
SELECT EXTRACT(MONTH FROM createdAt) AS month, SUM(price) AS total
FROM orders
WHERE YEAR(createdAt) = 2023
GROUP BY month
ORDER BY month;

This query returns:

monthtotal
180.00
7230.00
990.00
  1. Using MONTH()
SELECT MONTH(createdAt) AS month, SUM(price) AS total
FROM orders
WHERE YEAR(createdAt) = 2023
GROUP BY month
ORDER BY month;

And this returns the same previous result.

Group by Day in a Month of a Year

To calculate the total sales per day in July 2023:

  1. Using DATE_FORMAT()
SELECT DATE_FORMAT(createdAt, '%Y-%m-%d') AS date, SUM(price) AS total
FROM orders
WHERE YEAR(createdAt) = 2023 AND MONTH(createdAt) = 7
GROUP BY date
ORDER BY date;

This query returns:

datetotal
2023-07-09150.00
2023-07-1080.00
  1. Using DATE()
SELECT DATE(createdAt) AS day_date, SUM(price) AS total
FROM orders
WHERE YEAR(createdAt) = 2023 AND MONTH(createdAt) = 7
GROUP BY day_date
ORDER BY day_date;

The same result.

  1. Using EXTRACT()
SELECT EXTRACT(DAY FROM createdAt) AS day, SUM(price) AS total
FROM orders
WHERE YEAR(createdAt) = 2023 AND MONTH(createdAt) = 7
GROUP BY day
ORDER BY day;

This query returns:

daytotal
9150.00
1080.00
  1. Using DAY()
SELECT DAY(createdAt) AS day, SUM(price) AS total
FROM orders
WHERE YEAR(createdAt) = 2023 AND MONTH(createdAt) = 7
GROUP BY day
ORDER BY day;

And this returns the same previous result.

Conclusion

In this article, we learned how to group data by a specific date interval, year, month, and day.

And to do so, we used some functions:

  • DATE_FORMAT()
  • EXTRACT()
  • YEAR(), MONTH(), DAY(), and DATE()

Think about it

If you enjoyed this article, I’d truly appreciate it if you could share it—it really motivates me to keep creating more helpful content!

If you’re interested in exploring more, check out these articles.

Thanks for sticking with me until the end—I hope you found this article valuable and enjoyable!

Want more dev insights like this? Subscribe to get practical tips, tutorials, and tech deep dives delivered to your inbox. No spam, unsubscribe anytime.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top