MySQL Query GROUP BY day / month / year

MySQL Query GROUP BY day / month / year

Asked on November 30, 2018 in SQL Server.
Add Comment


  • 5 Answer(s)

    Try this solution,

    GROUP BY YEAR(record_date), MONTH(record_date)
    

    If you want, you can check the date and time functions in MySQL.

    Answered on November 30, 2018.
    Add Comment

    Here’s your answer,

    GROUP BY DATE_FORMAT(record_date, '%Y%m')
    
    

    You can get the solution by try this code.

    Answered on November 30, 2018.
    Add Comment

    This is pretty simple, once you find out the logic of below code you can get the solution for the questions which similarly like this.

    I can use the WHERE keyword inĀ  this code, so it becomes like this,

    SELECT COUNT(id)
    FROM stats
    WHERE YEAR(record_date) = 2009
    GROUP BY MONTH(record_date)
    
    Answered on November 30, 2018.
    Add Comment

    COUNT(id) and GROUP BY is a good choice for doing that. In the following example, the date column is “dat”.

    Here is how to get the number grouped by years:

    SELECT COUNT(id), YEAR(dat) FROM tab GROUP BY YEAR(dat);

    Here is how to get the number grouped by months:

    SELECT COUNT(id), MONTH(dat), YEAR(dat)
    FROM tab GROUP BY YEAR(dat), MONTH(dat);

    And here is how to get the number grouped by days:

    SELECT COUNT(id), DAY(dat), MONTH(dat), YEAR(dat)
    FROM tab GROUP BY YEAR(dat), MONTH(dat), DAY(dat);
    Answered on January 14, 2019.
    Add Comment
    SELECT COUNT(id)
    FROM stats
    GROUP BY EXTRACT(YEAR_MONTH FROM record_date)
    Answered on January 14, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.