千锋教育-做有情怀、有良心、有品质的职业教育机构

手机站
千锋教育

千锋学习站 | 随时随地免费学

千锋教育

扫一扫进入千锋手机站

领取全套视频
千锋教育

关注千锋学习站小程序
随时随地免费学习课程

当前位置:首页  >  技术干货  > mysql怎么查询连续时间段的最大值?

mysql怎么查询连续时间段的最大值?

来源:千锋教育
发布人:xqq
时间: 2023-10-14 01:44:42 1697219082

一、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、虽然简单,但实际上是一种强有力的语言,灵活使用其语言元素,可以进行非常复杂和高级的数据库操作。

声明:本站稿件版权均属千锋教育所有,未经许可不得擅自转载。
10年以上业内强师集结,手把手带你蜕变精英
请您保持通讯畅通,专属学习老师24小时内将与您1V1沟通
免费领取
今日已有369人领取成功
刘同学 138****2860 刚刚成功领取
王同学 131****2015 刚刚成功领取
张同学 133****4652 刚刚成功领取
李同学 135****8607 刚刚成功领取
杨同学 132****5667 刚刚成功领取
岳同学 134****6652 刚刚成功领取
梁同学 157****2950 刚刚成功领取
刘同学 189****1015 刚刚成功领取
张同学 155****4678 刚刚成功领取
邹同学 139****2907 刚刚成功领取
董同学 138****2867 刚刚成功领取
周同学 136****3602 刚刚成功领取
相关推荐HOT