gpt4 book ai didi

MySQL 选择开启和关闭时间戳之间的最大值加上 3 秒

转载 作者:可可西里 更新时间:2023-11-01 08:08:41 29 4
gpt4 key购买 nike

有一个运输车入口阀传感器,其数据被收集到 MySQL 数据库表中,显示带有时间戳的入口阀打开和关闭周期。还有一个活重传感器,其数据从相关运输机收集到 MySQL 数据库表中,显示活重值随着时间戳的变化。

我针对此表数据的一小部分构建了以下简化示例查询,以显示我需要使用另一个子查询等进一步操作的数据,以将数据放入临时表中。

MySQL 查询临时表

CREATE TABLE TempBatch

SELECT TimeStr,
CASE
WHEN Value = 'On' THEN 'On'
WHEN Value = 'Off' THEN 'Off'
ELSE Null END Status,
CASE
WHEN Value = 'On' THEN Null
WHEN Value = 'Off' THEN Null
ELSE Value END Weight
FROM
(
SELECT TimeStr, If(Value = 1, 'Off', 'On') Value FROM transporter2_inlet_valve_closed WHERE Value = 1 OR Value = 0
UNION ALL
SELECT TimeStr, Value FROM transporter2_liveweight
) x
WHERE TimeStr BETWEEN '2019-06-29 18:51:00' AND DATE_ADD('2019-06-29 19:27:18', INTERVAL 3 SECOND);

挣扎...

我正在努力寻找一种方法来轻松地显示记录,在该记录中我只能看到每个开启和关闭周期之间的最大重量,但在关闭周期的时间戳之后添加 3 秒以找到该周期的最大重量。

澄清 SQL 可能...

(SELECT MAX(Value) Value FROM transporter2_liveweight WHERE TimeStr BETWEEN StopTime AND StartTime)

我想要的...

基本上我想省略 Null 状态,但显示每个 OnOff 周期之间的最大权重值(+3 秒后 Off 时间戳)在 Off 状态记录的 Weight 列中具有最大权重,但保留 On 记录 Weight 列为 Null

示例期望结果

+---------------------+--------+---------+
| TimeStr | Status | Weight |
+---------------------+--------+---------+
| 2019-06-29 18:51:00 | On | Null |
| 2019-06-29 18:51:16 | Off | 315 |
| 2019-06-29 19:00:57 | On | Null |
| 2019-06-29 19:01:13 | Off | 311 |
+---------------------+--------+---------+

enter image description here


这里有一小部分 MySQL 数据供您查询和使用....

注意:创建下表并插入数据,这可用于尝试获得我所询问的所需结果。

CREATE TABLE `tempbatch` (
`TimeStr` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`Status` varchar(3) DEFAULT NULL,
`Weight` varchar(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 19:01:16',NULL,'311');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 19:01:15',NULL,'310');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 19:01:14',NULL,'309');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 19:01:13','Off',NULL);
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 19:01:12',NULL,'307');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 19:01:11',NULL,'305');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 19:01:10',NULL,'304');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 19:01:09',NULL,'305');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 19:01:07',NULL,'306');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 19:01:06',NULL,'308');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 19:01:05',NULL,'284');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 19:01:04',NULL,'256');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 19:01:03',NULL,'168');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 19:01:02',NULL,'106');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 19:01:01',NULL,'15');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 19:00:57','On',NULL);
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:57:30',NULL,'1');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:55:21',NULL,'0');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:55:11',NULL,'2');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:54:25',NULL,'1');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:53:55',NULL,'0');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:53:50',NULL,'2');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:53:34',NULL,'1');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:53:19',NULL,'0');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:53:16',NULL,'2');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:53:06',NULL,'1');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:52:49',NULL,'0');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:52:47',NULL,'-5');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:52:46',NULL,'-4');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:52:44',NULL,'-1');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:52:42',NULL,'0');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:52:41',NULL,'5');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:52:40',NULL,'6');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:52:39',NULL,'7');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:52:38',NULL,'8');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:52:37',NULL,'9');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:52:36',NULL,'10');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:52:35',NULL,'12');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:52:34',NULL,'14');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:52:32',NULL,'17');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:52:31',NULL,'19');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:52:30',NULL,'22');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:52:29',NULL,'25');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:52:28',NULL,'29');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:52:27',NULL,'32');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:52:26',NULL,'33');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:52:25',NULL,'34');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:52:24',NULL,'36');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:52:23',NULL,'41');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:52:22',NULL,'45');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:52:21',NULL,'47');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:52:20',NULL,'52');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:52:18',NULL,'53');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:52:17',NULL,'56');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:52:16',NULL,'63');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:52:15',NULL,'68');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:52:14',NULL,'73');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:52:13',NULL,'78');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:52:12',NULL,'84');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:52:11',NULL,'87');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:52:10',NULL,'90');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:52:09',NULL,'92');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:52:08',NULL,'94');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:52:07',NULL,'99');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:52:06',NULL,'104');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:52:05',NULL,'109');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:52:04',NULL,'113');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:52:03',NULL,'111');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:52:02',NULL,'114');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:52:01',NULL,'126');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:52:00',NULL,'132');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:51:59',NULL,'139');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:51:58',NULL,'145');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:51:56',NULL,'152');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:51:55',NULL,'158');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:51:54',NULL,'163');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:51:53',NULL,'165');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:51:52',NULL,'167');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:51:51',NULL,'173');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:51:50',NULL,'183');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:51:49',NULL,'192');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:51:47',NULL,'194');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:51:46',NULL,'196');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:51:45',NULL,'204');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:51:44',NULL,'202');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:51:43',NULL,'212');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:51:42',NULL,'219');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:51:41',NULL,'229');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:51:40',NULL,'232');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:51:39',NULL,'235');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:51:38',NULL,'237');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:51:37',NULL,'242');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:51:36',NULL,'246');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:51:34',NULL,'249');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:51:33',NULL,'252');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:51:32',NULL,'256');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:51:31',NULL,'267');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:51:29',NULL,'277');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:51:28',NULL,'283');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:51:27',NULL,'286');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:51:26',NULL,'285');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:51:24',NULL,'286');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:51:23',NULL,'300');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:51:22',NULL,'303');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:51:21',NULL,'309');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:51:20',NULL,'308');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:51:19',NULL,'314');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:51:18',NULL,'315');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:51:17',NULL,'311');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:51:16','Off',NULL);
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:51:14',NULL,'308');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:51:13',NULL,'310');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:51:12',NULL,'312');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:51:11',NULL,'311');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:51:10',NULL,'309');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:51:09',NULL,'305');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:51:08',NULL,'319');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:51:07',NULL,'260');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:51:06',NULL,'193');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:51:05',NULL,'108');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:51:04',NULL,'8');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:51:02',NULL,'-5');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:51:01',NULL,'20');
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:51:00','On',NULL);
INSERT INTO `TempBatch` (`TimeStr`,`Status`,`Weight`) VALUES ('2019-06-29 18:51:00',NULL,'-17');

最佳答案

下面的查询选择状态不为空的所有行。

对于状态为“关闭”的行,它使用子查询返回最近“打开”和当前“关闭”+ 3 秒之间的最大权重

select t.TimeStr, t.Status,
case
when status = 'Off'
then (select max(cast(Weight as signed)) from tempbatch t2
where t2.TimeStr between
(select max(TimeStr)
from tempbatch t3
where status = 'On' and t3.TimeStr < t.TimeStr)
and date_add(t.TimeStr, interval 3 second)
)
else Weight
end as Weight
from tempbatch t
where t.Status is not null
order by TimeStr

http://sqlfiddle.com/#!9/f27fb/6

关于MySQL 选择开启和关闭时间戳之间的最大值加上 3 秒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56821968/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com