mysql statistics weekly data in a year

Recently, the company wants to count the number of registered users, sales volume, and transaction volume every week within a year. Share the problems encountered in the development. Please forgive me if there are any mistakes in the article

MySQL  week number statistics  (week function )

First, let's talk about MySQL database statistics using week(Str/date,number)  The first parameter is a string or date, and the second parameter is a number

Next, I will introduce an example of the use of the week function

select week('2015-11-17',1) November 17 , 2015 is the 47th week in the new year

 

The value of the second parameter number in the week function is used

Number

first day of the week

Scope

Week 1 is the first week

0

Sunday

0~53

There is a Sunday in the year

1

on Monday

0~53

There are more than 3 days in the current year

2

Sunday

1~53

There is a Sunday in the year

3

on Monday

1~53

There are more than 3 days in the current year

4

Sunday

0~53

There are more than 3 days in the current year

5

on Monday

0~53

There is a Monday in the year

6

Sunday

1~53

There are more than 3 days in the current year

7

on Monday

1~53

There is a Monday in the year

WEEK('2015-11-17') uses only one parameter, and its second parameter is the default value of default_week_format , which defaults to  0 in MySQL

The following is the sql used to count the number of registered users per week during the development process

where post_date is the field of the registration time

 

SELECT WEEK(u.post_date,1),COUNT(*) 

FROM USERS u WHERE 1=1

AND  u.post_date>='2015-01-01'

AND u.post_date<='2015-12-31'

GROUP BY WEEK(u.post_date,1)

 

Note: The week parameter uses to match our normal week number display

Mysql also has a function of WEEKOFYEAR(d)

Week of the year. The range is from 1 to 53 , equivalent to WEEK(d,3) .

But for some special cases, he will have some problems, such as querying December 31 , 2014 , using weekofyear , he will regard that day as the first week of 2015




 
 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326692627&siteId=291194637