MySql分時間段統計

2021-09-25 07:39:28 字數 880 閱讀 4775

​ 在統計業務辦理人數的時候,需要從task_201907表裡面統計7月內每一天辦理的人數, 網羅上查到大家用格式化時間戳的方法,剛開始還沒看懂,原來是利用的from_unixtime函式。

查詢邏輯: 表中有時間字段,按照時間(天)分組統計參與人數。

問題:如果直接按照時間(datetime)字段分組,是精確到秒的, 那就變成統計每一秒的參與人數了,所以需要把時間變成天區分度的。

解決方法: date_format(created_at,』%y%m%d』) 或者是from_unixtime(unix_timestamp(created_at), 『%y%m%d』)

解決:

select from_unixtime(unix_timestamp(created_at), '%y%m%d') as days , count(distinct product_no)

from task_201907

group by days

select date_format(created_at,'%y%m%d') as days , count(product_no)

from t_mission_task_201907

group by days

用到的函式

from_unixtime(unix_timestamp), from_unixtime(unix_timestamp,format) :把時間戳格式化成時間

unix_timestamp() , unix_timestamp(date) :把時間轉化成時間戳,若無引數呼叫,則等同於unix_timestamp(now())。

date_format(date,format) :根據format 字串安排date 值的格式。

分時間段查詢

declare t table 時間 datetime,金額 int insert t select 2007 1 1 10 00 23 8 union all select 2007 1 1 01 00 04 4 union all select 2007 1 1 01 00 14 4 union...

MySQL統計函式記錄 時間段統計

按年彙總,統計 select sum mymoney as totalmoney,count as sheets from mytable group by date format col,y 按月彙總,統計 select sum mymoney as totalmoney,count as she...

mysql按照時間段內 每天統計

sql select t.report time,count report time from select date format report time,y m d as report time from report count as t group by t.report time date...