Group by month and year in MySQL

Group by month and year in MySQL

Asked on December 25, 2018 in Mysql.
Add Comment


  • 6 Answer(s)

    Simply use this one line code for groupby the month and year in Mysql.

    GROUP BY YEAR(t.summaryDateTime), MONTH(t.summaryDateTime) DESC;
    
    
    Answered on December 25, 2018.
    Add Comment

    One can achieve that groupby month and year easily by using this code,

    GROUP BY DATE_FORMAT(summaryDateTime,'%Y-%m')
    
    
    Answered on December 25, 2018.
    Add Comment

    Try to use this code in Mysql ,

    SELECT
        MONTHNAME(t.summaryDateTime) as month, YEAR(t.summaryDateTime) as year
    FROM
        trading_summary t
    GROUP BY EXTRACT(YEAR_MONTH FROM t.summaryDateTime) DESC";
    
    Answered on December 25, 2018.
    Add Comment

    You must do something like this

    SELECT onDay, id, 
    sum(pxLow)/count(*),sum(pxLow),count(`*`),
    CONCAT(YEAR(onDay),"-",MONTH(onDay)) as sdate 
    FROM ... where stockParent_id =16120 group by sdate order by onDay
    Answered on January 16, 2019.
    Add Comment

    This is for the derived table to be used with the main query – which will use MAX(firstcount) etc. THe main query will also have to group by the year and month fields.

    1. (SELECT YEAR(date1) AS yr, MONTH(date1) AS m, COUNT(date1) AS firstcount, 0 AS secondcount, 0 AS firstsum
    2. FROM registrations
    3. GROUP BY YEAR(date1), MONTH(date1)
    4. UNION
    5. SELECT YEAR(date2) AS yr, MONTH(date2) AS m, 0 AS firstcount, COUNT(date2) AS secondcount, SUM(price) AS firstsum
    6. FROM orders
    7. GROUP BY YEAR(date2), MONTH(date2)) AS derivedtable
    Answered on January 16, 2019.
    Add Comment
    GROUP BY YEAR(t.summaryDateTime), MONTH(t.summaryDateTime) DESC;
    Answered on February 25, 2019.
    Add Comment


  • Your Answer

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