一、mysql怎么查詢連續(xù)時(shí)間段的最大值
按儀器與時(shí)間(處理成小時(shí))group by,計(jì)算值的數(shù)量與和,再根據(jù)結(jié)果判斷值數(shù)量是否有缺失值,以及和的最大值。首先要明確采集標(biāo)準(zhǔn),比如說一分鐘采集一條記錄,那么可以group by 小時(shí)。
– Step1 創(chuàng)建表
CREATE TABLE monitor(
id int not null auto_increment,
seq_no int,
add_time DATETIME,
stat int,
primary key(id)
);
— Step2 初始化記錄,這里的6點(diǎn)和7點(diǎn)的數(shù)據(jù)完整,其中6點(diǎn)的有重復(fù)記錄。
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 查詢不完整的,這里加去重是為了剔除重復(fù)記錄。
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
/*
結(jié)果
seq_no date_hour record_cnt
2?? 2021-06-10#8? 39
*/
— scenario1,按天查詢固定小時(shí)周期內(nèi)總和的最大值,如果有重復(fù)數(shù)據(jù)需加邏輯去重(當(dāng)前未考慮)
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)
/*
結(jié)果
date_only max_daily
2021-06-10 3289
*/
— 3 針對任意小時(shí)的,建議通過存儲過程(定義起始時(shí)間、時(shí)間比較跨度)結(jié)合窗口函數(shù)(:= 模擬窗口函數(shù))處理
— 補(bǔ)注 1 當(dāng)前腳本用了隨機(jī)數(shù),關(guān)于字段state的統(tǒng)計(jì)結(jié)果不固定。
—????? 2 當(dāng)前演示數(shù)據(jù)庫是mysql 5.6.14。
延伸閱讀:
二、什么是數(shù)據(jù)庫和數(shù)據(jù)庫管理系統(tǒng)
數(shù)據(jù)庫的應(yīng)用非常廣泛,舉個(gè)例子,我們平時(shí)在瀏覽器上搜索內(nèi)容,就要用到數(shù)據(jù)庫去檢索我們的關(guān)鍵字。以前我們可能會用數(shù)組、集合、文件等來存儲數(shù)據(jù),但是接下來我們就會面臨一個(gè)問題,當(dāng)存儲的數(shù)據(jù)或內(nèi)容過多的時(shí)候,我們?nèi)绾稳ゾ珳?zhǔn)的找到我們需要的東西,這時(shí)候數(shù)據(jù)庫管理系統(tǒng)就派上了用場。除此之外,數(shù)據(jù)庫管理系統(tǒng)還能永久的儲存我們的數(shù)據(jù)。
為了便于大家理解,這里先給大家講解幾個(gè)概念
DB數(shù)據(jù)庫(database):存儲數(shù)據(jù)的“倉庫”。它保存了一系列有組織的數(shù)據(jù)。
DBMS數(shù)據(jù)庫管理系統(tǒng)(Database Management System):數(shù)據(jù)庫是通過DBMS創(chuàng)建和操作的容器。
SQL,結(jié)構(gòu)化查詢語言(Structured Query Language)用一句話概括,SQL是一種特殊目的的編程語言,一種專門用來與數(shù)據(jù)庫通信的語言。在數(shù)據(jù)庫中,數(shù)據(jù)被結(jié)構(gòu)化并存儲在不同的表中,從而簡化了訪問,更新和操作數(shù)據(jù)的過程。該表由列和行組成。數(shù)據(jù)庫中的表可以在關(guān)系的幫助下進(jìn)行連接。要在數(shù)據(jù)庫中執(zhí)行與數(shù)據(jù)相關(guān)的任務(wù),可以使用SQL。SQL代表結(jié)構(gòu)化查詢語言,旨在在特定RDBMS內(nèi)創(chuàng)建,修改和管理數(shù)據(jù)庫中的數(shù)據(jù)。
SQL優(yōu)點(diǎn):
1、不是某個(gè)特定數(shù)據(jù)庫供應(yīng)商專有的語言,幾乎所有DBMS(數(shù)據(jù)庫管理系統(tǒng))都支持SQL
2、簡單易學(xué)
3、雖然簡單,但實(shí)際上是一種強(qiáng)有力的語言,靈活使用其語言元素,可以進(jìn)行非常復(fù)雜和高級的數(shù)據(jù)庫操作。