Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL grouping by week, based on a date column?

Tags:

sql

mysql

I have a table with a date column and I would like to try and group by, using a week as a time reference in order to count how many rows occured per week. I have done this for days, using GROUP BY Date(Date_Column) but i'm unsure how to do this by week?

Thanks

like image 302
James Avatar asked Jun 30 '11 10:06

James


2 Answers

SELECT ...
FROM ....
GROUP BY YEAR(Date_column), WEEKOFYEAR(Date_Column);
like image 70
CristiC Avatar answered Oct 05 '22 23:10

CristiC


Try to put a GROUP BY YEARWEEK(date_column) at the end of your query - this will take in consideration also the year the date is in.

like image 21
Tudor Constantin Avatar answered Oct 05 '22 22:10

Tudor Constantin