mysql十分钟分组_MYSQL每隔10分钟进行分组统计的实现方法
前言
本文的内容主要是介绍了mysql每隔10分钟进行分组统计的实现方法,在画用户登录、操作情况在一天内的分布图时会非常有用,之前我只知道用「存储过程」实现的方法(虽然执行速度快,但真的是太不灵活了),后来学会了用高级点的「group by」方法来灵活实现类似功能。
正文:
-- 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」进行区分,但是方法有所不同,前者只能针对10分钟(或1小时)级别,后者可以动态调整间隔大小,两者效率差不多,可以根据实际情况选用
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()
# 或 由给定格式字符串转换成
# 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))
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。
希望与广大网友互动??
点此进行留言吧!
mysql十分钟分组_MYSQL每隔10分钟进行分组统计的实现方法相关推荐
- oracle 按每分钟分组,oracle按每个10分钟进行分组展示数据
例如 有这么一张表 XATXDAY_FLIGHT(航班飞航表), 有这么一个字段 STD_LOCAL(起飞时间), 要求:统计一天24小时之内每隔10分钟,这10分钟之内有几架飞机起飞. 比如:XAT ...
- 定时任务每隔10分钟
0 */10 * * * 0 0/10 * * * 区别:第一个表示服务启动后每10分钟执行一次:第二个表示服务启动后也是每隔10分钟执行一次,但是是整点,比如10分.20分等. 比如服务是10:07 ...
- mysql降低数据库版本_三步10分钟搞定数据库版本的降迁 (将后台数据库SQL2008R2降为SQ...
三步10分钟搞定数据库版本的降迁 (将SQL2008R2降为SQL2005版本) 前思后想仍觉得实战数据库版本的降迁一文中的方式不仅老土而且低效,故有了下文三步搞定数据库从MSSQL2008R2 高版 ...
- 十分钟计算机说课稿,10分钟说课稿课件.ppt
10分钟说课稿课件 儿歌<我是一粒米> 米饭的来历 古诗<锄禾> * 河南·济源 邵原镇实验小学 王应应 <秋天的收获> 说课 人民教育出版社二年级上册<品德 ...
- 10分钟带你了解python_ComeOn!10分钟带你了解Python的变量和数据类型
对任何语言来说,变量和数据类型都是非常重要和基础的内容.这篇文章就带你用10分钟的时间,学会Python的变量和数据类型这个知识点. 一.知识点 python 关键字 变量的定义与赋值 input() ...
- 手冲1分钟正常吗_甩脂10分钟=慢跑1个小时?懒人减肥甩脂机靠谱吗?
有朋友开玩笑说: 我要瘦成一道闪电, 闪瞎旁人的双眼! 谁知却胖成了一堵墙, 挡住了人们的视线! 不过, 有不少商家就瞄准了 减肥这个消费热点, 推出了各种各样的减肥产品, 比如:甩脂机. (图片来自 ...
- 十分钟学python-【译】10分钟学会Pandas
十分钟学会Pandas 这是关于Pandas的简短介绍主要面向新用户.你可以参考Cookbook了解更复杂的使用方法 习惯上,我们这样导入: In [1]: importpandas as pd In ...
- mysql十大报错_MySQL十大报错函数
1.Floor() round() 遵循四舍五入把原值转化为指定小数位数,如:round(1.45,0) = 1;round(1.55,0)=2 floor()向下舍入为指定小数位数 如:floor( ...
- mysql回滚用法_Mysql误操作后利用binlog2sql快速回滚的方法详解
前言 在日常工作或者学习中,操作数据库时候难免会因为"大意"而误操作,需要快速恢复的话通过备份来恢复是不太可能的,下面这篇文章主要给大家介绍关于Mysql误操作后利用binlog2 ...
最新文章
- asp.net Core多环境读取Json
- base64/32/16编码
- 3、Swing布局管理器
- Artifactory——启动错误[Artifactory failed to initialize: check Artifactory logs for errors.]解决方案
- 结合MSDN理解windows service 服务安装的三个类。
- vue引入id3_vue常见知识点
- jeecms v9导入myeclipse 2015 ehcache.xml报错问题
- Oracle 数据库、实例、表空间、用户、数据库对象
- Android -- 自定义View小Demo,绘制四位数随机码(一)
- eclipse 安装egit插件
- 简单树匹配算法STM-理论篇
- No pubspec.yaml file found. This command should be run from the root of your Flutter project. Do not
- 链表的中间结点-python
- Pentaho bi 中文文档
- 连接mysql超时_数据库连接超时的原因及其解决方法
- 腾讯计算机安全实验室,TRP-AI反病毒引擎创新:腾讯安全最新成果入围顶级学术会议...
- 北京理工大学计算机科学与技术培养方案,王全玉_北京理工大学计算机学院
- UESTC论坛-清水河畔自动登陆/重复发贴/安全性分析
- linux 补充文件名,linux修改文件名(Linux 下使用 shell 批量修改文件名的三种方法)...
- fidder classic 界面介绍
热门文章
- gdi在固定范围内绘图_寿光计算机绘图CAD学费诚信经营
- VS如何更改项目类型?
- 视频编码中的RC(rate control)是什么?码率控制 CBR (Constant Bit Rate)、VBR (Variable Bit Rate)
- python pycharm如何全局(整个项目中)搜索指定代码?(CTRL+SHIFT+F)全局字符串搜索
- Intel Realsense 如何获取已连接所有摄像头的序列号参数?context() query_devices() size() camera_info device_list
- DVWA--SQL注入
- PAT甲级题目翻译+答案 AcWing(高精度)
- 修建道路 贪心,思维(女赛)
- 计算机组成原理中lad什么意思,计算机组成原理的大神们能不能帮忙做几道题啊...
- mysql 查询每天 如果没有显示为0_「15」MySQL的系统信息函数