1

I have rows of data in a database that have a DATETIME called date on. There is another column called points.

I need to count all the points for each month and return a list of total points for each month in order of month with most points down.

I'm not really sure where to start when it comes to selecting rows by month?

SuperBiasedMan
  • 9,814
  • 10
  • 45
  • 73
sark9012
  • 5,485
  • 18
  • 61
  • 99
  • 1
    possible duplicate of [MySQL Query GROUP BY day / month / year](http://stackoverflow.com/questions/508791/mysql-query-group-by-day-month-year) – postelrich Sep 29 '15 at 15:57

1 Answers1

1

You can extract the month and year from the date using monthname (or month) and year, respectively, and group by them:

SELECT   YEAR(`date`), MONTHNAME(`date`), SUM(`points`)
FROM     mytable
GROUP BY YEAR(`date`), MONTHNAME(`date`)
ORDER BY 3 DESC

Note that if your table represents a single year, or you want to calculate a multi-year total, you should omit the year expression from the select list and the group by clause.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • I was up for helping when opening this question. Now you helped me hah. Monthname will seriously be useful for me. Upped. – C4d Sep 29 '15 at 16:03