gpt4 book ai didi

mysql - 从一个表中获取带有时间戳的最大值加上另一个表中某个时间段的 3 秒

转载 作者:行者123 更新时间:2023-11-29 07:19:51 25 4
gpt4 key购买 nike

hopper2_setpoint_met 表值为 1 时,我需要用 TimeStr 值显示 EndTime,并且我需要将 Weight 显示为 TimeStr +3 secondhopper2_liveweight 表中的 MAX(Value)当设定点满足表值为 1 时,从 TimeStr 值开始。

示例期望结果

+---------------------+--------+
| EndTime | Weight |
+---------------------+--------+
| 2019-06-30 12:02:33 | 300 |
| 2019-06-30 12:04:45 | 299 |
+---------------------+--------+

enter image description here


数据和我的尝试

您可以从此 SQLFiddle 访问此数据发布我构建模式并插入一小部分数据的位置,或者如果您想使用它在自己的 MySQL 数据库表上创建,那么这里也有相应的 SQL。

CREATE TABLE `hopper2_setpoint_met` (
`TimeStr` datetime NOT NULL,
`Value` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `hopper2_setpoint_met` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:02:33',1);
INSERT INTO `hopper2_setpoint_met` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:03:51',0);
INSERT INTO `hopper2_setpoint_met` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:04:45',1);
INSERT INTO `hopper2_setpoint_met` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:05:56',0);
INSERT INTO `hopper2_setpoint_met` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:06:49',1);

CREATE TABLE `hopper2_liveweight` (
`TimeStr` datetime NOT NULL,
`Value` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:02:33',292);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:02:34',298);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:02:35',300);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:02:36',300);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:04:14',106);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:04:15',111);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:04:16',116);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:04:17',123);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:04:19',132);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:04:20',142);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:04:21',148);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:04:22',154);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:04:23',160);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:04:24',166);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:04:25',172);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:04:26',177);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:04:27',184);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:04:29',190);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:04:30',195);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:04:31',201);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:04:32',208);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:04:33',213);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:04:34',220);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:04:35',226);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:04:36',232);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:04:37',239);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:04:38',244);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:04:39',251);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:04:40',257);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:04:41',263);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:04:42',269);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:04:43',276);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:04:44',282);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:04:45',288);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:04:46',293);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:04:47',299);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:04:48',299);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:06:18',108);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:06:19',112);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:06:20',117);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:06:21',123);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:06:22',128);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:06:23',135);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:06:24',141);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:06:25',148);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:06:26',154);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:06:27',160);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:06:28',165);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:06:29',172);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:06:30',177);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:06:31',183);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:06:32',190);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:06:33',196);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:06:34',201);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:06:35',208);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:06:36',213);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:06:37',220);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:06:38',226);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:06:39',231);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:06:40',237);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:06:41',242);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:06:42',248);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:06:43',254);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:06:44',262);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:06:45',269);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:06:46',276);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:06:47',282);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:06:49',288);

我发布了 MySQL to select max value between on and off time stamps plus 3 seconds昨天关于运输车的类似事情,但我了解到使用临时表并不是最好的解决方案,尽管我在那里收到的答案确实给了我要求的预期结果。

我还从收到的这些答案中学到了一些东西,并尝试了这些东西的几种变体,例如 If(CAST(Value AS UNSIGNED) != 0, Value, Null) Weight 技巧和我的新任务的其他一些事情,我再次无法获得预期的结果。

如果我将数据放入临时表中,我当然可以得到预期的结果,但它表现不佳,我相信它也会使过程复杂化,而且并不是真正需要的。

我尝试了几种不同的变体,使用 JOIN 语句和 UNION 数据以及子查询,但当我试图在 timestr+3 之间获得最大权重时失败了第二个记录。

我很想学习一些新东西,并且一个灯泡从一个有助于解决这个问题的答案中消失了,所以向前推进,这是一个应用类似逻辑而不需要创建临时或永久临时表的问题。

如果需要,将其全部保存在一个带有一些 JOIN 语句的查询中,或者甚至多个子查询中,这对我来说会很有效。我不确定使用 MySQL session 变量是否有帮助,但我什至尝试了一些东西但失败了。

最佳答案

一个适合的查询是:

SELECT sm.TimeStr AS EndTime, Max(lw.Value) AS Weight
FROM (SELECT TimeStr FROM hopper2_setpoint_met WHERE Value = 1) sm
INNER JOIN hopper2_liveweight lw ON (sm.TimeStr + INTERVAL 3 SECOND) = lw.TimeStr
GROUP BY sm.TimeStr
;

关于mysql - 从一个表中获取带有时间戳的最大值加上另一个表中某个时间段的 3 秒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56828199/

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