记录时间戳10秒的一部分

时间:2017-05-30 22:18:34

标签: mysql sql

我在运行MySQL的表中有一些SQL数据,下面是我使用MySQL WorkBench导出时的样子。

我看到了Count number of rows that are not within 10 seconds of each other帖子,并尝试在此处相应地应用它,但我无法轻易弄明白,所以我发布了一些问题作为帮助。

开始数据

这是我在表

中开始的数据
  

列标题

     

TimeInt,TimeStr,IsInItValue,Value,IQuality

数据

'1495542477', '2017-05-23 12:27:57', '0', '0', '3'
'1495542475', '2017-05-23 12:27:55', '0', '1', '3'
'1495542474', '2017-05-23 12:27:54', '0', '3', '3'
'1495542473', '2017-05-23 12:27:53', '0', '4', '3'
'1495542472', '2017-05-23 12:27:52', '0', '5', '3'
'1495542471', '2017-05-23 12:27:51', '0', '4', '3'
'1495542470', '2017-05-23 12:27:50', '0', '3', '3'
'1495447612', '2017-05-22 10:06:52', '0', '1', '3'
'1495447611', '2017-05-22 10:06:51', '0', '2', '3'
'1494851001', '2017-05-15 12:23:21', '0', '2', '3'
'1493819613', '2017-05-03 13:53:33', '0', '0', '3'
'1493819612', '2017-05-03 13:53:32', '0', '1', '3'
'1493819611', '2017-05-03 13:53:31', '0', '2', '3'
'1493819609', '2017-05-03 13:53:29', '0', '4', '3'
'1493819608', '2017-05-03 13:53:28', '0', '6', '3'
'1493819607', '2017-05-03 13:53:27', '0', '5', '3'
'1493819606', '2017-05-03 13:53:26', '0', '4', '3'
'1493819605', '2017-05-03 13:53:25', '0', '2', '3'
'1493819603', '2017-05-03 13:53:23', '0', '1', '3'

我想知道是否有办法将这些数据连接到自身或将某些逻辑应用于子查询等,这样我就可以获得值或者很容易区分下一行时间戳的数据在此表中大于10秒。

我上面链接的帖子上的一个答案的方法使用了一种方法,其中NULL值的记录会有所帮助,但我无法使用MySQL。

我在Count number of rows that are not within 10 seconds of each other帖子上尝试了答案的变体,但无法弄清楚我出错的地方,但是在我试过的几件事之一。我不确定interval是否仍然有效,因为那是从2011年开始,或者我正在做其他不正确的事情以获得语法错误,但这是我尝试过的事情之一。

SELECT t2.TimeStr, Value, 
(SELECT MAX(t.TimeStr) 
        FROM canouncebit t 
        WHERE t.TimeStr > t2.TimeStr
          AND t.TimeStr - t2.TimeStr <= interval '10' second) NextRecord
FROM canouncebit t2
ORDER BY TimeStr

预期数据(或足够接近的数据)

我想最终得到这样的东西,甚至有点接近我可以使用

数据

'1495542477', '2017-05-23 12:27:57', '0', '0', '3'
'1495447612', '2017-05-22 10:06:52', '0', '1', '3'
'1494851001', '2017-05-15 12:23:21', '0', '2', '3'
'1493819613', '2017-05-03 13:53:33', '0', '0', '3'

所以基本上我只需要返回数据,特别是只有那个值已经存在超过X秒的记录(在这种情况下为10)。

创建表语句

CREATE TABLE `ContainerOzBit` (
  `TimeInt` varchar(10) NOT NULL,
  `TimeStr` datetime NOT NULL,
  `IsInitValue` int(11) NOT NULL,
  `Value` float NOT NULL,
  `IQuality` int(11) NOT NULL,
  UNIQUE KEY `TimeInt` (`TimeInt`,`TimeStr`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8$$

插入数据声明

INSERT INTO `ContainerOzBit` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1495542477','2017-05-23 12:27:57',0,0,3);
INSERT INTO `ContainerOzBit` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1495542475','2017-05-23 12:27:55',0,1,3);
INSERT INTO `ContainerOzBit` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1495542474','2017-05-23 12:27:54',0,3,3);
INSERT INTO `ContainerOzBit` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1495542473','2017-05-23 12:27:53',0,4,3);
INSERT INTO `ContainerOzBit` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1495542472','2017-05-23 12:27:52',0,5,3);
INSERT INTO `ContainerOzBit` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1495542471','2017-05-23 12:27:51',0,4,3);
INSERT INTO `ContainerOzBit` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1495542470','2017-05-23 12:27:50',0,3,3);
INSERT INTO `ContainerOzBit` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1495447612','2017-05-22 10:06:52',0,1,3);
INSERT INTO `ContainerOzBit` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1495447611','2017-05-22 10:06:51',0,2,3);
INSERT INTO `ContainerOzBit` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1494851001','2017-05-15 12:23:21',0,2,3);
INSERT INTO `ContainerOzBit` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1493819613','2017-05-03 13:53:33',0,0,3);
INSERT INTO `ContainerOzBit` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1493819612','2017-05-03 13:53:32',0,1,3);
INSERT INTO `ContainerOzBit` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1493819611','2017-05-03 13:53:31',0,2,3);
INSERT INTO `ContainerOzBit` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1493819609','2017-05-03 13:53:29',0,4,3);
INSERT INTO `ContainerOzBit` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1493819608','2017-05-03 13:53:28',0,6,3);
INSERT INTO `ContainerOzBit` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1493819607','2017-05-03 13:53:27',0,5,3);
INSERT INTO `ContainerOzBit` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1493819606','2017-05-03 13:53:26',0,4,3);
INSERT INTO `ContainerOzBit` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1493819605','2017-05-03 13:53:25',0,2,3);
INSERT INTO `ContainerOzBit` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1493819603','2017-05-03 13:53:23',0,1,3);

1 个答案:

答案 0 :(得分:1)

一种方法是利用MySQL的会话变量:

-- omit the duplicated timeint that we used in the derived table
SELECT c.* FROM (
  -- grab the maximum timeint value in the group
  SELECT MAX(timeint) timeint FROM (
    SELECT timeint, -- current row's timeint value
           -- if diff betwen current and prev values more than 10 sec 
           -- increment the group number, otherwise keep it the same
           @g := IF(timeint - @p > 10, @g + 1, @g) g, 
           -- preserve the the value so it's available on the next iteration
           @p := timeint 
      FROM ContainerOzBit CROSS JOIN (
        SELECT @p := NULL, @g := 1 -- initialize sesion variables
      ) i
     -- deterministic order is crucial for this approach
     -- since we're iteration row by row
     ORDER BY timeint
  ) q
  -- group by the group number 
   GROUP BY g
   -- since timeint values are unique 
   -- join back and retrieve all the columns
) r JOIN ContainerOzBit c 
   ON r.timeint = c.timeint
   -- set the reverse order for the result set
 ORDER BY timeint DESC;

阅读最内部选择向外的评论

结果:

+------------+---------------------+-------------+-------+----------+
| TimeInt    | TimeStr             | IsInitValue | Value | IQuality |
+------------+---------------------+-------------+-------+----------+
| 1495542477 | 2017-05-23 12:27:57 |           0 |     0 |        3 |
| 1495447612 | 2017-05-22 10:06:52 |           0 |     1 |        3 |
| 1494851001 | 2017-05-15 12:23:21 |           0 |     2 |        3 |
| 1493819613 | 2017-05-03 13:53:33 |           0 |     0 |        3 |
+------------+---------------------+-------------+-------+----------+

这是dbfiddle演示