开发者

MySQL grouping by week, based on a date column?

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 pe开发者_如何学Pythonr week. I have done this for days, using GROUP BY Date(Date_Column) but i'm unsure how to do this by week?

Thanks


SELECT ...
FROM ....
GROUP BY YEAR(Date_column), WEEKOFYEAR(Date_Column);


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.


SELECT week(Date_Column)
FROM ....
GROUP BY week(Date_Column);


 SELECT WEEKOFYEAR("2017-01-01"),YEARWEEK("2017-01-01"),WEEK("2017-01-01");

Outputs:

WEEKOFYEAR("2017-01-01")    YEARWEEK("2017-01-01")  WEEK("2017-01-01")
52                          201701                  1

Looks like YEARWEEK is the best solution. No need to concat the year.


SELECT CONCAT(YEAR(Date_Column),'/',WEEK(Date_Column)) AS efdt 
FROM ....
GROUP BY efdt;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜