MySQL 8: How to Group Data by Day, Month, and Year

Updated: January 26, 2024 By: Guest Contributor Post a comment

Introduction

Understanding how to group temporal data is a cornerstone of data analysis and reporting. It allows for sensible aggregation of information over time and can provide insights into trends, peaks, and troughs in data. MySQL, as a leading relational database management system, offers powerful functions for aggregating data based on time intervals such as day, month, and year.

In this tutorial, we’re going to explore how to effectively group data by day, month, and year in MySQL 8. We’ll start with basic examples and gradually move on to more advanced techniques, providing outputs for the code examples where applicable. Let’s dive in!

Getting Started with Time-Based Grouping

To begin with, let’s assume that we have a table called ‘sales’ that records transactions, with a ‘date’ column of the type DATETIME or DATE that indicates when each sale occurred.

CREATE TABLE sales (
    id INT AUTO_INCREMENT PRIMARY KEY,
    product_id INT,
    quantity INT,
    sale_date DATETIME
);

Let’s insert some sample data into our ‘sales’ table for demonstration purposes:

INSERT INTO sales (product_id, quantity, sale_date) VALUES
(1, 10, '2023-01-01 14:35:00'),
(2, 15, '2023-01-02 09:20:00'),
(3, 12, '2023-02-01 16:15:00'),
(4, 20, '2023-02-01 10:05:00'),
(5, 5, '2023-03-01 07:30:00');

Grouping by Day

To group our sales by day, we use MySQL’s DATE() function to extract the date part from the ‘sale_date’ column and the GROUP BY statement:

SELECT DATE(sale_date) AS sale_day, COUNT(*) AS total_sales
FROM sales
GROUP BY sale_day;

This query will group all sales by their day, ignoring the time part, and count the total sales for each day.

Output: 2019-01-01 | 10 2019-01-02 | 15 2019-02-01 | 32 2019-03-01 | 5

Grouping by Month

When we need to aggregate data by month, MySQL provides the YEAR() and MONTH() functions. Here’s a basic example:

SELECT YEAR(sale_date) AS sale_year, MONTH(sale_date) AS sale_month, SUM(quantity) AS total_quantity
FROM sales
GROUP BY sale_year, sale_month;

This query sums the ‘quantity’ of sales for each month, grouped by year and month.

Output: 2019 | 1 | 25 2019 | 2 | 32 2019 | 3 | 5

Grouping by Year

To group results by year, we use the YEAR() function on our date column:

SELECT YEAR(sale_date) AS sale_year, COUNT(*) AS total_transactions
FROM sales
GROUP BY sale_year;

This simple query counts the number of transactions per year.

Output: 2019 | 62

Advanced Groupings

Group by Week

Sometimes, groupings are needed by the week of the year. This is a bit more advanced and useful in many business scenarios:

SELECT YEARWEEK(sale_date) AS sale_yearweek, SUM(quantity) AS total_quantity
FROM sales
GROUP BY sale_yearweek;

Combining Groupings

We can combine multiple time units to gain a deeper understanding of our sales trends. Here’s an example:

SELECT
    YEAR(sale_date) AS sale_year,
    MONTH(sale_date) AS sale_month,
    DAY(sale_date) AS sale_day,
    SUM(quantity) AS total_quantity
FROM sales
GROUP BY sale_year, sale_month, sale_day;

Using GROUP_CONCAT for Detail

The GROUP_CONCAT function can concatenate group values into a summary string:

SELECT
    MONTH(sale_date) AS sale_month,
    GROUP_CONCAT(quantity ORDER BY sale_date) AS quantities
FROM sales
GROUP BY sale_month;

Dealing with NULL and Adding WHERE Clauses

In some cases, your date column might have NULL values, or you may want to filter results before grouping. The WHERE clause comes in handy in such scenarios:

SELECT
    DATE(sale_date) AS sale_day,
    SUM(quantity) AS total_quantity
FROM sales
WHERE sale_date IS NOT NULL
GROUP BY sale_day;

Conclusion

In this tutorial, we’ve explored the power of grouping in MySQL to provide meaningful summaries of data over various time periods. Through multiple examples, we have demonstrated the capability of MySQL 8 to group data by day, month, and year – which can form the basis of many reporting and analytical tasks. The approaches discussed herein are flexible and adaptable to various practical scenarios, enabling in-depth understanding and decision-making based on time-bound data trends.