views:

30

answers:

2

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()?

A: 

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) 
shamittomar
i want i'm just concern at week. counting data every 7 days.what the different "Week()" and "Day() between 1 and 7"?
klox
A: 

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).

Jonathan Leffler
how about at this http://stackoverflow.com/questions/3640729/counting-data-and-grouping-by-week-in-mysql. It use DAY() for count weekly. What's the different?
klox
@klox: I should probably go and downvote that answer. The DAY() function is a synonym for DAYOFMONTH. That means the queries shown group the data from days 1-7 of January with the data for days 1-7 of February, and March, and April, and ... of each year over which the data extends. Apparently, it answers the OP's question, but I'm somewhat sceptical that they'll be happy when their data extends over a couple of years. There's more than one way to answer any particular query - there are some differences between that question and this.
Jonathan Leffler
@klox: oh, I see you are the poster over in the other Q...I recommend reconsidering your acceptance of that answer.
Jonathan Leffler