主頁 > 知識庫 > MYSQL每隔10分鐘進(jìn)行分組統(tǒng)計的實(shí)現(xiàn)方法

MYSQL每隔10分鐘進(jìn)行分組統(tǒng)計的實(shí)現(xiàn)方法

熱門標(biāo)簽:東莞外呼企業(yè)管理系統(tǒng) 地圖簡圖標(biāo)注 谷歌地圖標(biāo)注位置圖解 沈陽智能外呼系統(tǒng)供應(yīng)商 清遠(yuǎn)申請400電話 南通電銷外呼系統(tǒng)哪家強(qiáng) 桂林云電銷機(jī)器人收費(fèi) 如何選擇優(yōu)質(zhì)的外呼系統(tǒng) 手機(jī)外呼系統(tǒng)違法嗎

前言

本文的內(nèi)容主要是介紹了MYSQL每隔10分鐘進(jìn)行分組統(tǒng)計的實(shí)現(xiàn)方法,在畫用戶登錄、操作情況在一天內(nèi)的分布圖時會非常有用,之前我只知道用「存儲過程」實(shí)現(xiàn)的方法(雖然執(zhí)行速度快,但真的是太不靈活了),后來學(xué)會了用高級點(diǎn)的「group by」方法來靈活實(shí)現(xiàn)類似功能。

正文:

-- time_str '2016-11-20 04:31:11'
-- date_str 20161120

select concat(left(date_format(time_str, '%y-%m-%d %h:%i'),15),'0') as time_flag, count(*) as count from `security`.`cmd_info` where `date_str`=20161120 group by time_flag order by time_flag; -- 127 rows

select round(unix_timestamp(time_str)/(10 * 60)) as timekey, count(*) from `security`.`cmd_info` where `date_str`=20161120 group by timekey order by timekey; -- 126 rows

-- 以上2個SQL語句的思路類似——使用「group by」進(jìn)行區(qū)分,但是方法有所不同,前者只能針對10分鐘(或1小時)級別,后者可以動態(tài)調(diào)整間隔大小,兩者效率差不多,可以根據(jù)實(shí)際情況選用

select concat(date(time_str),' ',hour(time_str),':',round(minute(time_str)/10,0)*10), count(*) from `security`.`cmd_info` where `date_str`=20161120 group by date(time_str), hour(time_str), round(minute(time_str)/10,0)*10; -- 145 rows

select concat(date(time_str),' ',hour(time_str),':',floor(minute(time_str)/10)*10), count(*) from `security`.`cmd_info` where `date_str`=20161120 group by date(time_str), hour(time_str), floor(minute(time_str)/10)*10; -- 127 rows (和 date_format 那個等價)

select concat(date(time_str),' ',hour(time_str),':',ceil(minute(time_str)/10)*10), count(*) from `security`.`cmd_info` where `date_str`=20161120 group by date(time_str), hour(time_str), ceil(minute(time_str)/10)*10; -- 151 rows


DELIMITER //

DROP PROCEDURE IF EXISTS `usp_cmd_info`;

CREATE PROCEDURE `usp_cmd_info`(IN dates VARCHAR(12))
BEGIN
 SELECT count(*) from `cmd_info` where `time_str` BETWEEN CONCAT(dates, " 00:00:00") AND CONCAT(dates, " 00:10:00") INTO @count_0;
 SELECT count(*) from `cmd_info` where `time_str` BETWEEN CONCAT(dates, " 00:10:00") AND CONCAT(dates, " 00:20:00") INTO @count_1;
 ...
 SELECT count(*) from `cmd_info` where `time_str` BETWEEN CONCAT(dates, " 23:40:00") AND CONCAT(dates, " 23:50:00") INTO @count_142;
 SELECT count(*) from `cmd_info` where `time_str` BETWEEN CONCAT(dates, " 23:50:00") AND CONCAT(dates, " 23:59:59") INTO @count_143;
 select @count_0, @count_1, @count_2, @count_3, @count_4, @count_5, @count_6, @count_7, @count_8, @count_9, @count_10, @count_11, @count_12, @count_13, @count_14, @count_15, @count_16, @count_17, @count_18, @count_19, @count_20, @count_21, @count_22, @count_23, @count_24, @count_25, @count_26, @count_27, @count_28, @count_29, @count_30, @count_31, @count_32, @count_33, @count_34, @count_35, @count_36, @count_37, @count_38, @count_39, @count_40, @count_41, @count_42, @count_43, @count_44, @count_45, @count_46, @count_47, @count_48, @count_49, @count_50, @count_51, @count_52, @count_53, @count_54, @count_55, @count_56, @count_57, @count_58, @count_59, @count_60, @count_61, @count_62, @count_63, @count_64, @count_65, @count_66, @count_67, @count_68, @count_69, @count_70, @count_71, @count_72, @count_73, @count_74, @count_75, @count_76, @count_77, @count_78, @count_79, @count_80, @count_81, @count_82, @count_83, @count_84, @count_85, @count_86, @count_87, @count_88, @count_89, @count_90, @count_91, @count_92, @count_93, @count_94, @count_95, @count_96, @count_97, @count_98, @count_99, @count_100, @count_101, @count_102, @count_103, @count_104, @count_105, @count_106, @count_107, @count_108, @count_109, @count_110, @count_111, @count_112, @count_113, @count_114, @count_115, @count_116, @count_117, @count_118, @count_119, @count_120, @count_121, @count_122, @count_123, @count_124, @count_125, @count_126, @count_127, @count_128, @count_129, @count_130, @count_131, @count_132, @count_133, @count_134, @count_135, @count_136, @count_137, @count_138, @count_139, @count_140, @count_141, @count_142, @count_143;
END //

DELIMITER ;

show PROCEDURE status\G

CALL usp_cmd_info("2016-10-20");

上面的這段MySQL存儲過程的語句非常長,不可能用手工輸入,可以用下面的這段Python代碼按所需的時間間隔自動生成:

import datetime

today = datetime.date.today()
# 或 由給定格式字符串轉(zhuǎn)換成
# today = datetime.datetime.strptime('2016-11-21', '%Y-%m-%d')

min_today_time = datetime.datetime.combine(today, datetime.time.min) # 2016-11-21 00:00:00
max_today_time = datetime.datetime.combine(today, datetime.time.max) # 2016-11-21 23:59:59

sql_procedure_arr = []
sql_procedure_arr2 = []
for x in xrange(0, 60*24/5, 1):
  start_datetime = min_today_time + datetime.timedelta(minutes = 5*x)
  end_datetime = min_today_time + datetime.timedelta(minutes = 5*(x+1))
  # print x, start_datetime.strftime("%Y-%m-%d %H:%M:%S"), end_datetime.strftime("%Y-%m-%d %H:%M:%S")
  select_str = 'SELECT count(*) from `cmd_info` where `time_str` BETWEEN "{0}" AND "{1}" INTO @count_{2};'.format(start_datetime, end_datetime, x)
  # print select_str
  sql_procedure_arr.append(select_str)
  sql_procedure_arr2.append('@count_{0}'.format(x))
print '\n'.join(sql_procedure_arr)
print 'select {0};'.format(', '.join(sql_procedure_arr2))

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。

您可能感興趣的文章:
  • MySQL 統(tǒng)計查詢實(shí)現(xiàn)代碼
  • MYSQL中有關(guān)SUM字段按條件統(tǒng)計使用IF函數(shù)(case)問題
  • 如何使用MySQL查詢某個列中相同值的數(shù)量統(tǒng)計
  • 利用MySQL統(tǒng)計一列中不同值的數(shù)量方法示例
  • 單個select語句實(shí)現(xiàn)MySQL查詢統(tǒng)計次數(shù)
  • MySQL中幾種數(shù)據(jù)統(tǒng)計查詢的基本使用教程
  • mysql按照天統(tǒng)計報表當(dāng)天沒有數(shù)據(jù)填0的實(shí)現(xiàn)代碼
  • MySQL按時間統(tǒng)計數(shù)據(jù)的方法總結(jié)
  • mysql 獲取規(guī)定時間段內(nèi)的統(tǒng)計數(shù)據(jù)
  • MySQL統(tǒng)計函數(shù)GROUP_CONCAT使用陷阱分析
  • mysql滑動訂單問題原理與解決方法實(shí)例分析

標(biāo)簽:內(nèi)蒙古 成都 臨沂 天津 常德 重慶 貴州 湖州

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《MYSQL每隔10分鐘進(jìn)行分組統(tǒng)計的實(shí)現(xiàn)方法》,本文關(guān)鍵詞  MYSQL,每隔,10分鐘,進(jìn)行,分組,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《MYSQL每隔10分鐘進(jìn)行分組統(tǒng)計的實(shí)現(xiàn)方法》相關(guān)的同類信息!
  • 本頁收集關(guān)于MYSQL每隔10分鐘進(jìn)行分組統(tǒng)計的實(shí)現(xiàn)方法的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章