Mysql进行数据分布统计经典专用SQL
2019-06-04 00:58
1586 查看
版权声明:阅读优秀源码,宛若一场探索未知的旅行,疑惑处惊奇,优雅处旖旎; 一切都是新奇的,千回百转与大师的心灵触碰,一场跨越时空的对话,涤荡了原有的愚昧,蜕变出更好的自己。 https://blog.csdn.net/FENGQIYUNRAN/article/details/90761696
1. 数据库建表语句
[code]CREATE TABLE `start_duration` ( `id` bigint(13) NOT NULL AUTO_INCREMENT COMMENT '自增id', `name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL COMMENT '事件名称', `device_id` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL COMMENT '设备id', `country` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL COMMENT '国家', `start_time` int(11) NOT NULL DEFAULT '0' COMMENT '开始时间', `duration` int(11) NOT NULL DEFAULT '0' COMMENT '时间间隔', `create_time` datetime DEFAULT NULL COMMENT '创建时间', `update_time` datetime DEFAULT NULL COMMENT '更新时间', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4
2. 统计 duration的数据分布
[code]SELECT case when duration >0 and duration <500 then '1:(0,500)' when duration >=500 and duration <1000 then '2:[500,1000)' when duration >=1000 and duration <2000 then '3:[1000,2000)' when duration >=2000 and duration <3000 then '4:[2000,3000)' when duration >=3000 and duration <5000 then '5:[3000,5000)' when duration >=5000 and duration <10000 then '6:[5000, 10000)' when duration >=10000 and duration <20000 then '7:[10000, 20000)' when duration >=20000 then '8:[20000+)' ELSE '1:(0,500)' END 'duration(ms)', COUNT(*) 'count' FROM start_duration WHERE create_time > '2019-06-01 00:00:00' GROUP BY case when duration >0 and duration <500 then '1:(0,500)' when duration >=500 and duration <1000 then '2:[500,1000)' when duration >=1000 and duration <2000 then '3:[1000,2000)' when duration >=2000 and duration <3000 then '4:[2000,3000)' when duration >=3000 and duration <5000 then '5:[3000,5000)' when duration >=5000 and duration <10000 then '6:[5000, 10000)' when duration >=10000 and duration <20000 then '7:[10000, 20000)' when duration >=20000 then '8:[20000+)' ELSE '1:(0,500)' END
3. 统计执行结果
[code]+------------------+-------+ | duration(ms) | count | +------------------+-------+ | 1:(0,500) | 14896 | | 2:[500,1000) | 12847 | | 3:[1000,2000) | 11132 | | 4:[2000,3000) | 4603 | | 5:[3000,5000) | 1459 | | 6:[5000, 10000) | 449 | | 7:[10000, 20000) | 204 | | 8:[20000+) | 156 | +------------------+-------+
分析:在每个分布的字段前面天机 “数字:” 格式可以充分利用,mysql的自动排序功能,使数据展现更美观整齐
相关文章推荐
- Mysql分表数据通过Shell进行导出与统计
- 怎么样用PHPMyAdmin 将*.Sql数据倒入MYSQL[经典推荐]
- Mysql中对数据统计进行排行榜排行
- 怎么样用PHPMyAdmin 将*.Sql数据倒入MYSQL[经典推荐]
- Linux下通过php对Mysql中的数据进行定时统计并入库
- mysql排序,可以对统计的数据进行排序
- Python利用groupby模块进行Mysql分表数据的汇总统计
- 怎么样用PHPMyAdmin 将*.Sql数据倒入MYSQL[经典推荐]
- MYSQL经典SQL之时长统计
- SQL对数据进行统计、常用集合函数
- MYSQL 巧用count,sum进行统计数据
- MySQL的经典用法(二)----按月统计数据
- vc-mysql-sniffer统计MySQL的SQL分布
- Mysql将查询后的数据进行排名的SQL语句
- 【ORACLE】一个简单实用的对数据进行行列转换并统计的SQL语句
- Local Response Normalization作用——感觉LRN也是解决的梯度消失和爆炸问题,统计机器学习中的一个经典假设是“源空间(source domain)和目标空间(target domain)的数据分布(distribution)是一致的
- SQL数据进行排序、分组、统计10技巧
- 【ORACLE】一个简单实用的对数据进行行列转换并统计的SQL语句
- 按指定上下限区间进行数据统计的示例.sql
- hist函数 进行 数据 分布 统计