这是我在下面讨论的所有内容创建的SQL Fiddle,但此查询将专门设置为仅查询同一天的记录,例如2017-11-29
所以这个例子就足够了。
我有两个数据表
当我在BTR表中只有一个特定日期的值时,我总是得到预期的结果(即0
,1 of 25
等等。
如果一天中有多个“批次运行”记录集(根据示例数据)并且其值重置为另一个有效值,但同一天具有不同的和更新的时间戳,
如何阻止加入比较新/当前/最新BTR记录时间戳更早的BC记录以防止无效结果SQL Fiddle我提供的示例在{SQL Fiddle的记录中启动此问题1}}时间戳(2 of 25
)?
我不希望BC表中的旧记录与BTR表中的下一个/更新/最新时间戳记记匹配,并希望它根据时间移动到下一个真实记录值,因此当存在较新的BTR记录时,不要看那些比那个时间更长的东西,直到另一个新纪录。
创建表格和插入数据
2017-11-29T13:59:59Z
加入查询
TimeStr
因此,根据我提供的中的结果集,CREATE TABLE `batchestorun` (
`TimeInt` varchar(10) NOT NULL,
`TimeStr` datetime NOT NULL,
`IsInitValue` int(11) NOT NULL,
`Value` int(11) NOT NULL,
`IQuality` int(11) NOT NULL,
UNIQUE KEY `uk_Times` (`TimeInt`,`TimeStr`)
);
CREATE TABLE `batchescomplete` (
`TimeInt` varchar(10) NOT NULL,
`TimeStr` datetime NOT NULL,
`IsInitValue` int(11) NOT NULL,
`Value` int(11) NOT NULL,
`IQuality` int(11) NOT NULL,
UNIQUE KEY `uk_Times` (`TimeInt`,`TimeStr`)
);
## BTR
INSERT INTO `batchestorun` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1511917564','2017-11-29 01:06:04',1,0,0);
INSERT INTO `batchestorun` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1511938976','2017-11-29 07:02:56',0,21,3);
INSERT INTO `batchestorun` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1511962086','2017-11-29 13:28:06',0,0,3);
INSERT INTO `batchestorun` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1511962089','2017-11-29 13:28:09',0,1,3);
INSERT INTO `batchestorun` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1511962979','2017-11-29 13:42:59',0,20,3);
INSERT INTO `batchestorun` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1511971912','2017-11-29 16:11:52',0,0,3);
INSERT INTO `batchestorun` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1511971934','2017-11-29 16:12:14',0,25,3);
## BC
INSERT INTO `batchescomplete` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1511917564','2017-11-29 01:06:04',1,0,0);
INSERT INTO `batchescomplete` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1511939767','2017-11-29 07:16:07',0,1,3);
INSERT INTO `batchescomplete` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1511940230','2017-11-29 07:23:50',0,2,3);
INSERT INTO `batchescomplete` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1511940692','2017-11-29 07:31:32',0,3,3);
INSERT INTO `batchescomplete` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1511942121','2017-11-29 07:55:21',0,4,3);
INSERT INTO `batchescomplete` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1511944099','2017-11-29 08:28:19',0,5,3);
INSERT INTO `batchescomplete` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1511945511','2017-11-29 08:51:51',0,6,3);
INSERT INTO `batchescomplete` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1511947447','2017-11-29 09:24:07',0,7,3);
INSERT INTO `batchescomplete` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1511948408','2017-11-29 09:40:08',0,8,3);
INSERT INTO `batchescomplete` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1511949620','2017-11-29 10:00:20',0,9,3);
INSERT INTO `batchescomplete` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1511951571','2017-11-29 10:32:51',0,10,3);
INSERT INTO `batchescomplete` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1511952520','2017-11-29 10:48:40',0,11,3);
INSERT INTO `batchescomplete` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1511953011','2017-11-29 10:56:51',0,12,3);
INSERT INTO `batchescomplete` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1511953960','2017-11-29 11:12:40',0,13,3);
INSERT INTO `batchescomplete` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1511954905','2017-11-29 11:28:25',0,14,3);
INSERT INTO `batchescomplete` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1511955860','2017-11-29 11:44:20',0,15,3);
INSERT INTO `batchescomplete` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1511956351','2017-11-29 11:52:31',0,16,3);
INSERT INTO `batchescomplete` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1511959198','2017-11-29 12:39:58',0,17,3);
INSERT INTO `batchescomplete` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1511960194','2017-11-29 12:56:34',0,18,3);
INSERT INTO `batchescomplete` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1511960731','2017-11-29 13:05:31',0,19,3);
INSERT INTO `batchescomplete` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1511961677','2017-11-29 13:21:17',0,20,3);
INSERT INTO `batchescomplete` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1511962086','2017-11-29 13:28:06',0,0,3);
INSERT INTO `batchescomplete` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1511962849','2017-11-29 13:40:49',0,1,3);
INSERT INTO `batchescomplete` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1511963999','2017-11-29 13:59:59',0,2,3);
INSERT INTO `batchescomplete` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1511965321','2017-11-29 14:22:01',0,3,3);
INSERT INTO `batchescomplete` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1511971912','2017-11-29 16:11:52',0,0,3);
INSERT INTO `batchescomplete` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1511971935','2017-11-29 16:12:15',0,1,3);
列记录从第一行的SELECT
IF(btr.TimeStr<= bc.TimeStr, CONCAT(bc.Value," of ",btr.Value), '') Batch
, bc.TimeStr
FROM BatchesComplete bc
INNER JOIN BatchesToRun btr ON btr.TimeStr <= bc.TimeStr
WHERE btr.Value >1 AND bc.Value >0 AND btr.IQuality = 3;
#SELECT * FROM batchestorun;
#SELECT * FROM batchescomplete;
#SELECT * FROM batchestorun WHERE Value >1;
#SELECT * FROM batchescomplete WHERE Value >0;
到第20行的Batch
都是正确的。
之后的行与1 of 21
的下一个20 of 21
表BTR
字段开始时相关,结果显示Value
“ BC < / strong>“20
字段与之前的” BTR “和新的” BTR “。
虽然这同时得到2017-11-29 13:59:59
和Value
,但我需要它只显示2 of 20
下一个/更新的“ BTR ”值而不回头看自最新“ BTR ”表2 of 21
记录的2 of 20
以来加入任何旧记录。
另一个预期结果示例
所以在我的例子中的前20条记录之后,我想要结果 看起来像这样 - 越接近越好但我甚至可以鞭打东西 如果你能帮我把事情搞得一团糟。
表关系
{{3}}
答案 0 :(得分:1)
我想我明白你要做什么。您需要进行子选择以从BTR检索MAX(timestr)并使用它来进行连接。我稍微重新安排了一下这个问题,但我认为这可能是你想要的:
SELECT bc.value, btr.value, bc.timestr, btr.timestr
FROM batchescomplete bc,
batchestorun btr
WHERE btr.timestr = (SELECT MAX(btr2.timestr) FROM batchestorun btr2 WHERE bc.timestr >= btr2.TimeStr and btr2.value > 1 and btr2.IQuality = 3)
AND bc.value > 0
答案 1 :(得分:0)
我假设您从“预期结果示例”视觉中想要以下列方式显示行:
Green BTR Row
Green BC Rows
Red BTR Row
Red BC Rows
Blue BTR Row
Blue BC Row
为此,您实际上不需要加入表。只需对每个表应用所需的过滤,将两个结果集与一个联合一起粉碎,然后按TimeStr排序:
SELECT * FROM batchestorun WHERE Value >1
union all
SELECT * FROM batchescomplete WHERE Value >0
order by timestr
结果: http://sqlfiddle.com/#!9/e94b86/34
如果我误解了您想要的输出,请告诉我。