简体   繁体   中英

How to count total row on MySQL based upon month and year

My database table contains value in this way IMAGE FOR TABLE DATA

I want to track down same email which has been used more than one times for a particular month and year. In the above scenario, email that has been repeated multiple times was sandeshphuya@gmail.com , jes@gmail.com and ramu@gmail.com for different months. I want to track down customer repetition following their email for each month and year.

The query that I am using right now is

SELECT DATE_FORMAT(booked_on, '%Y-%m') as monthYear, email,
COUNT(*) AS 'Count'
FROM 'tablename'
GROUP BY email,DATE_FORMAT(booked_on, '%Y-%m') HAVING COUNT(*) > 1 ORDER BY `booked_on`;

GROUP BY email was used as it generates the repeated email and GROUP BY DATE_FORMAT(booked_on, '%Y'-%m') was used to track down total email repeated for each month/year.

This prints out data as

IMAGE FOR SELECT QUERY

How can I track down total repeated email following month and year? The expected result is

RESULT EXPECTED

You can use your query as a subquery for a new group by:

select sub.monthYear,count(*)
from
    (SELECT DATE_FORMAT(booked_on, '%Y-%m') as monthYear, 
            email,
            COUNT(*) AS 'Count'
     FROM 'tablename'
     GROUP BY email,DATE_FORMAT(booked_on, '%Y-%m') 
              HAVING COUNT(*) > 1 
     ORDER BY `booked_on`) as sub
GROUP BY sub.monthYear

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM