mysql 每5分钟统计_SQL按时间段统计(5分钟统计一次访问量为例,oracle统计)
需求:統(tǒng)計當天的訪問量,每五分鐘采集一次
表結(jié)構(gòu)中有日期字段,類型TIMESTAMP
如果,統(tǒng)計是采用每秒/分鐘/小時/天/周/月/年,都非常容易實現(xiàn),只要to_char日期字段然后group by分組即可
但是:如果是X秒/分鐘/小時/天/周/月/年 and X>1,就需要變通實現(xiàn),方法如下:
方案一:臨時表/臨時存儲
統(tǒng)計每5分鐘的訪問量,存入臨時表或者臨時存儲(比如excel)
循環(huán)當天整個時間段
然后對臨時表/臨時存儲的數(shù)據(jù)做排序
方案二:偽列的SQL查詢(數(shù)據(jù)量小于<1億數(shù)據(jù)量,且時間列做索引)
分析:5分鐘一次,那就對每條記錄做個偽列,每5分鐘的數(shù)據(jù)都是一個標識。
比如:創(chuàng)建時間=201604061200-至-?201604061204,這五分鐘統(tǒng)一標識為201604061200
創(chuàng)建時間=201604061205?-至-?201604061209,這五分鐘統(tǒng)一標識為201604061205
實現(xiàn)起來非常方便:
a. 取出創(chuàng)建時間的分鐘參數(shù):to_char(t.create_time,'mi'),都是兩位:比如01分,07分,13分,19分....
只判斷第二位,如果第二位<5,統(tǒng)一讓分鐘的第二位=0,否則(第二位>=5)統(tǒng)一讓分鐘的第二位=5
而分鐘的第一位不變
比如01分、04分統(tǒng)一標識為00分,05、06、09分統(tǒng)一標識為05分
切割字符串使用substr函數(shù)
判斷使用case when
拼接使用||
b.取出創(chuàng)建時間的年月日小時參數(shù),拼裝上第一步的特殊分鐘參數(shù)
一個偽列就這么誕生了
關(guān)鍵SQL如下:
case when substr(to_char(t.create_time,'mi'),2,1)<5 then to_char(t.create_time,'yyyymmddhh24')||substr(to_char(t.create_time,'mi'),1,1)||0
else to_char(t.create_time,'yyyymmddhh24')||substr(to_char(t.create_time,'mi'),1,1)||5 end as newTime
上一個完整的sql
select count(tmp.id) totalNum,tmp.newTime
from(
select t.id,t.latest_status, -- ID,狀態(tài)
to_char(t.create_time,'yyyymmddhh24mi') oldTime, -- 原來的時間
case when substr(to_char(t.create_time,'mi'),2,1)<5 then to_char(t.create_time,'yyyymmddhh24')||substr(to_char(t.create_time,'mi'),1,1)||0
else to_char(t.create_time,'yyyymmddhh24')||substr(to_char(t.create_time,'mi'),1,1)||5 end as newTime -- 時間段偽列
from acquire_order t
where t.create_time>=to_date('20160406000000','yyyymmddhh24miss')
order by t.create_time asc
) tmp
group by tmp.newTime
order by totalNum desc
總結(jié)
以上是生活随笔為你收集整理的mysql 每5分钟统计_SQL按时间段统计(5分钟统计一次访问量为例,oracle统计)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 原型链面向对象----多态
- 下一篇: MySQL5.7.17免安装版本配置