Question

I have a web task for counting some data inside DB. I must count this data daily, weekly, and monthly.

For weekly, I am still confused to decide which MySQL syntax I should use for this case. There are two suggestions to resolve this problem: using DAY() or WEEK().

I want this syntax is not limited by a confusing thing. As should declare the first of the month and end of month or start in Sunday or Monday, etc. I want it every time cans for use (but focuses at weekly).

Which is the one that I can use? DAY() or WEEK()?

Was it helpful?

Solution

For the weekly statistics, you need to decide whether you want your weekly statistics to run from Monday to Sunday, or from Sunday to Saturday. Either can be supported.

Ultimately, you'll probably be grouping by both YEAR(datefield) and WEEK(datefield, 1). When it comes to sorting monthly, you'll be grouping by YEAR(datefield) and MONTH(datefield). For the daily statistics, if the datefield is stored in a DATE type, then you can simply group by that; if you have time too, you'll be grouping the values by YEAR(datefield) and DAYOFYEAR(datefield).

OTHER TIPS

The Week should start on Sunday/Monday and NOT on first of the month. So, you should use WEEK() preferably with Monday as first day of week:

   WEEK(date, 1) 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top