Group statistics by week, month, day, hour

http://my.oschina.net/mjRao/blog/698641

met, summed up, just a porter!

From Tao Weiji Wiki

select DATE_FORMAT(create_time,'%Y%u') weeks,count(caseid) count from tc_case group by weeks;

select DATE_FORMAT(create_time,'%Y%m') months,count(caseid) count from tc_case group by months;

select DATE_FORMAT(create_time,'%Y%m%d') days,count(caseid) count from tc_case group by days;  

select DATE_FORMAT(create_time,'%Y%m%d%H') hours,count(caseid) count from tc_case group by hours;


DATE_FORMAT(date,format)

formats the date value according to the format string. The following modifiers can be used in the format string:

%M month name (January...December)

%W week name (Sunday...Saturday)

%D day of the month with an English prefix (1st, 2nd, 3rd, etc. .)

%Y year, number, 4 digits

%y year, number, 2 digits

%a Abbreviated week name (Sun...Sat)

%d day of month, number (00...31)

%e day of month , number(0...31)

%m month, number(01...12)

%c month, number(1...12)

%b abbreviated month name (Jan...Dec)

%j number of days in a year ( 001…366)

%H hours (00…23)

%k hours (0…23)

%h hours (01…12)

%I hours (01…12)

%l hours (1…12)

%i minutes, numbers (00…59)

%r time, 12 hours (hh:mm:ss [AP]M)

%T time, 24 hours (hh:mm:ss)

%S seconds (00…59)

%s seconds (00...59)

%p AM or PM

%w the day of the week (0=Sunday...6=Saturday)

%U the week (0...52), where Sunday is the first day of the week

%u the week (0...52), where Monday is the week The first day of

%% A literal "%".

From danielyi

yearly summary, statistics:

select sum(mymoney) as totalmoney, count(*) as sheets from mytable group by date_format(col, '%Y');


Summary by month, statistics:

select sum(mymoney) as totalmoney, count(*) as sheets from mytable group by date_format(col, '%Y-%m');


Summary by quarter, statistics:

select sum(mymoney) as totalmoney,count(*) as sheets from mytable group by concat(date_format(col, '%Y'),FLOOR((date_format(col, '%m')+2)/3));


select sum(mymoney) as totalmoney,count(*) as sheets from mytable group by concat(date_format(col, '%Y'),FLOOR((date_format(col, '%m')+2)/3));


By hour:

select sum(mymoney) as totalmoney,count(*) as sheets from mytable group by date_format(col, '%Y-%m-%d %H ');


Query the data for the current year:

SELECT * FROM mytable WHERE year(FROM_UNIXTIME(my_time)) = year(curdate())


Query data with quarter numbers:

SELECT id, quarter(FROM_UNIXTIME(my_time)) FROM mytable;


To query the data for this quarter:

SELECT * FROM mytable WHERE quarter(FROM_UNIXTIME(my_time)) = quarter(curdate());


Statistics this month:

select * from mytable where month(my_time1) = month(curdate()) and year(my_time2) = year(curdate())


Statistics this week:

select * from mytable where month(my_time1) = month(curdate()) and week(my_time2) = week(curdate())


Record within N days:

WHERE TO_DAYS(NOW())-TO_DAYS(time field)<=N

Guess you like

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