mysql怎么查询连续时间段的最大值?
一、mysql怎么查询连续时间段的最大值
按仪器与时间(处理成小时)group by,计算值的数量与和,再根据结果判断值数量是否有缺失值,以及和的最大值。首先要明确采集标准,比如说一分钟采集一条记录,那么可以group by 小时。
– Step1 创建表
CREATE TABLE monitor(
id int not null auto_increment,
seq_no int,
add_time DATETIME,
stat int,
primary key(id)
);
— Step2 初始化记录,这里的6点和7点的数据完整,其中6点的有重复记录。
INSERT INTO monitor(seq_no,add_time,stat)
SELECT 1,’2021-6-10 6:0′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 6:1′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 6:2′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 6:3′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 6:4′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 6:5′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 6:6′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 6:7′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 6:8′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 6:9′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 6:10′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 6:11′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 6:12′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 6:13′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 6:14′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 6:15′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 6:16′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 6:17′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 6:18′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 6:19′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 6:20′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 6:21′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 6:22′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 6:23′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 6:24′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 6:25′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 6:26′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 6:27′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 6:28′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 6:29′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 6:30′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 6:31′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 6:32′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 6:33′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 6:34′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 6:35′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 6:36′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 6:37′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 6:38′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 6:39′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 6:40′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 6:41′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 6:42′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 6:43′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 6:44′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 6:45′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 6:46′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 6:47′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 6:48′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 6:49′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 6:50′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 6:51′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 6:52′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 6:53′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 6:54′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 6:55′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 6:56′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 6:57′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 6:58′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 6:58′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 6:59′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 7:0′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 7:1′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 7:2′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 7:3′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 7:4′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 7:5′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 7:6′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 7:7′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 7:8′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 7:9′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 7:10′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 7:11′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 7:12′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 7:13′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 7:14′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 7:15′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 7:16′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 7:17′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 7:18′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 7:19′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 7:20′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 7:21′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 7:22′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 7:23′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 7:24′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 7:25′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 7:26′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 7:27′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 7:28′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 7:29′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 7:30′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 7:31′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 7:32′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 7:33′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 7:34′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 7:35′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 7:36′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 7:37′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 7:38′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 7:39′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 7:40′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 7:41′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 7:42′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 7:43′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 7:44′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 7:45′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 7:46′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 7:47′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 7:48′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 7:49′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 7:50′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 7:51′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 7:52′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 7:53′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 7:54′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 7:55′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 7:56′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 7:57′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 7:58′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 1,’2021-6-10 7:59′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 2,’2021-6-10 8:1′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 2,’2021-6-10 8:2′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 2,’2021-6-10 8:3′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 2,’2021-6-10 8:4′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 2,’2021-6-10 8:5′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 2,’2021-6-10 8:6′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 2,’2021-6-10 8:7′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 2,’2021-6-10 8:8′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 2,’2021-6-10 8:9′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 2,’2021-6-10 8:10′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 2,’2021-6-10 8:11′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 2,’2021-6-10 8:12′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 2,’2021-6-10 8:13′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 2,’2021-6-10 8:14′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 2,’2021-6-10 8:15′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 2,’2021-6-10 8:16′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 2,’2021-6-10 8:17′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 2,’2021-6-10 8:18′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 2,’2021-6-10 8:19′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 2,’2021-6-10 8:20′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 2,’2021-6-10 8:21′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 2,’2021-6-10 8:22′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 2,’2021-6-10 8:23′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 2,’2021-6-10 8:24′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 2,’2021-6-10 8:25′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 2,’2021-6-10 8:26′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 2,’2021-6-10 8:27′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 2,’2021-6-10 8:28′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 2,’2021-6-10 8:29′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 2,’2021-6-10 8:30′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 2,’2021-6-10 8:31′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 2,’2021-6-10 8:32′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 2,’2021-6-10 8:33′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 2,’2021-6-10 8:34′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 2,’2021-6-10 8:35′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 2,’2021-6-10 8:36′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 2,’2021-6-10 8:37′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 2,’2021-6-10 8:38′ ,FLOOR(1 + (RAND() * 101)) UNION ALL
SELECT 2,’2021-6-10 8:39′ ,FLOOR(1 + (RAND() * 101)) ;
— Step3 查询
— scenario1 查询不完整的,这里加去重是为了剔除重复记录。
SELECT seq_no,CONCAT(DATE(add_time),’#’,HOUR(add_time))date_hour,COUNT(DISTINCT add_time) record_cnt
FROM monitor A
GROUP BY seq_no,CONCAT(DATE(add_time),’#’,HOUR(add_time))
HAVING COUNT(DISTINCT add_time)<60
/*
结果
seq_no date_hour record_cnt
2 2021-06-10#8 39
*/
— scenario1,按天查询固定小时周期内总和的最大值,如果有重复数据需加逻辑去重(当前未考虑)
SELECT SUBSTR(date_hour,1,INSTR(date_hour,’#’)-1) date_only,MAX(sum_hour) max_daily
FROM
(
SELECT A.seq_no,B.date_hour,SUM(A.stat) sum_hour
FROM monitor A
JOIN(
SELECT seq_no,CONCAT(DATE(add_time),’#’,HOUR(add_time)) date_hour,COUNT(DISTINCT add_time) record_cnt
FROM monitor A
GROUP BY seq_no,CONCAT(DATE(add_time),’ ‘,HOUR(add_time))
HAVING COUNT(DISTINCT add_time)=60
)B
ON A.seq_no = B.seq_no
AND CONCAT(DATE(add_time),’#’,HOUR(add_time)) = B.date_hour
GROUP BY A.seq_no,B.date_hour
)C
GROUP BY SUBSTR(date_hour,1,INSTR(date_hour,’#’)-1)
/*
结果
date_only max_daily
2021-06-10 3289
*/
— 3 针对任意小时的,建议通过存储过程(定义起始时间、时间比较跨度)结合窗口函数(:= 模拟窗口函数)处理
— 补注 1 当前脚本用了随机数,关于字段state的统计结果不固定。
— 2 当前演示数据库是mysql 5.6.14。
延伸阅读:
二、什么是数据库和数据库管理系统
数据库的应用非常广泛,举个例子,我们平时在浏览器上搜索内容,就要用到数据库去检索我们的关键字。以前我们可能会用数组、集合、文件等来存储数据,但是接下来我们就会面临一个问题,当存储的数据或内容过多的时候,我们如何去精准的找到我们需要的东西,这时候数据库管理系统就派上了用场。除此之外,数据库管理系统还能永久的储存我们的数据。
为了便于大家理解,这里先给大家讲解几个概念
DB数据库(database):存储数据的“仓库”。它保存了一系列有组织的数据。
DBMS数据库管理系统(Database Management System):数据库是通过DBMS创建和操作的容器。
SQL,结构化查询语言(Structured Query Language)用一句话概括,SQL是一种特殊目的的编程语言,一种专门用来与数据库通信的语言。在数据库中,数据被结构化并存储在不同的表中,从而简化了访问,更新和操作数据的过程。该表由列和行组成。数据库中的表可以在关系的帮助下进行连接。要在数据库中执行与数据相关的任务,可以使用SQL。SQL代表结构化查询语言,旨在在特定RDBMS内创建,修改和管理数据库中的数据。
SQL优点:
1、不是某个特定数据库供应商专有的语言,几乎所有DBMS(数据库管理系统)都支持SQL
2、简单易学
3、虽然简单,但实际上是一种强有力的语言,灵活使用其语言元素,可以进行非常复杂和高级的数据库操作。
猜你喜欢LIKE
相关推荐HOT
更多>>MySQL普通索引不等于为什么会失效?
一、MySQL普通索引不等于会失效的原因1、列数据类型不匹配如果查询条件中的列类型与索引列类型不匹配,MySQL无法使用索引进行优化。例如,索引...详情>>
2023-10-18 11:14:01为什么关系型数据库系统不易于scaling out(横向扩展)?
一、为什么关系型数据库系统不易于scaling out(横向扩展)因为传统的SQL数据库没想到要分片存储,而现在的NewSQL开始考虑到这些问题了。当然分...详情>>
2023-10-18 10:49:50MySQL InnoDB引擎中的各种锁具体是怎么实现的?
一、MySQL InnoDB引擎中的各种锁的实现方式MySQL InnoDB引擎中的各种锁是通过多种机制实现的1、共享锁(Shared Lock)和排他锁(Exclusive Lock...详情>>
2023-10-18 10:26:59Android怎么连接远程数据库?
一、Android连接远程数据库的方法1、确保远程数据库可以远程访问首先,确保您的远程数据库允许远程连接。这通常需要在数据库服务器的配置中启用...详情>>
2023-10-18 10:09:10热门推荐
常用JS前端开发框架有哪些?
沸事务并发控制s2pl和s2pl有何区别?
热为什么一台电脑可以安装多个SQL Server实例?
热MySQL普通索引不等于为什么会失效?
新为什么关系型数据库系统不易于scaling out(横向扩展)?
MySQL InnoDB引擎中的各种锁具体是怎么实现的?
Android怎么连接远程数据库?
为什么MySQL在innodb引擎中即使使用了MVCC机制仍然会出现丢失更新?
IO多路复用中select、poll、epoll之间的区别?
读写分离为什么能够提升性能?
为什么mysql innodDB中组合索引中范围查询后的条件索引会失效?
为什么noteexpress不能建立数据库也不能打开别的数据库?
计算机前端是什么?
APP中集成移动端车牌识别系统都能达到什么效果?