noneJavaScript
noneJavaScript

Reputation: 845

How to aggregate dates by months or years Mysql

$SQLString = "SELECT    
            count(score) as counts,
            DATE(date),
            SUM(CASE WHEN gender = 1 then 1 ELSE 0 END) Male,
            SUM(CASE WHEN gender = 2 then 1 ELSE 0 END) Female,
            AVG(age) as age, score
            FROM persons  
            WHERE date > '".$_SESSION['date1']."' AND date < '".$_SESSION['date2']."' 
            GROUP BY DATE(date) 
            ORDER BY DATE(date) asc";   

This is my Query that display some data, like gender,age, etc per day. How could I aggregate those dates by months or years and display the added data?

Notice me if Im not explain my self as well :)

Upvotes: 3

Views: 3483

Answers (1)

Marc B
Marc B

Reputation: 360572

GROUP BY year(date), month(date).

Upvotes: 5

Related Questions