在MySQL中更改状态时获取开始和结束时间

时间:2018-12-03 21:20:18

标签: mysql unix-timestamp

我有一张带有设备及其状态的表。每5分钟将每个设备的状态保存到数据库中。数据库中的每个记录都包含设备的名称(ABC,DEF等。),设备的状态(1 = OK,6 =已关闭和11 =已计划关闭),将记录添加到数据库的时间以及添加记录的日期。

现在我想知道每台设备的状态更改的开始时间和结束时间。

我正在使用MySQL 5.6版。

 CREATE TABLE IF NOT EXISTS `demo` (
    `Id` INT(11) NOT NULL AUTO_INCREMENT,
    `Name` VARCHAR(255) NOT NULL,
    `StateN` INT(11) NOT NULL,
    `TimeAdded` INT(11) NOT NULL,
    `DateAdded` INT(11) NOT NULL,
    PRIMARY KEY (`Id`)
);

INSERT INTO `demo` (`Id`, `Name`, `StateN`, `TimeAdded`, `DateAdded`) VALUES
  (1, 'ABC', 1, 1540250401, 1540245600),
  (2, 'DEF', 1, 1540250401, 1540245600),
  (3, 'GHI', 1, 1540250401, 1540245600),
  (4, 'JKL', 11,1540250401, 1540245600),

  (5, 'ABC', 1, 1540250701, 1540245600),
  (6, 'DEF', 1, 1540250701, 1540245600),
  (7, 'GHI', 1, 1540250701, 1540245600),
  (8, 'JKL', 11, 1540250701, 1540245600),

  (9, 'ABC', 1, 1540251001, 1540245600),
  (10, 'DEF', 1, 1540251001, 1540245600),
  (11, 'GHI', 6, 1540251001, 1540245600),
  (12, 'JKL', 11, 1540251001, 1540245600),

  (13, 'ABC', 1, 1540251301, 1540245600),
  (14, 'DEF', 1, 1540251301, 1540245600),
  (15, 'GHI', 6, 1540251301, 1540245600),
  (16, 'JKL', 11, 1540251301, 1540245600),

  (17, 'ABC', 1, 1540251601, 1540245600),
  (18, 'DEF', 1, 1540251601, 1540245600),
  (19, 'GHI', 1, 1540251601, 1540245600),
  (20, 'JKL', 11,1540251601, 1540245600);

我想获得以下输出:

Name StateN StartTime  EndTime
ABC  1      1540250401 1540251601
DEF  1      1540250401 1540251601
GHI  1      1540250401 1540250701
GHI  6      1540251001 1540251301
GHI  1      1540251601 1540251601
JKL  11     1540250401 1540251601

2 个答案:

答案 0 :(得分:0)

这将返回与您要执行的操作类似的结果,这使我怀疑您的结果集中有错误...

SELECT name
     , staten
     , MIN(timeadded) starttime
     , MAX(timeadded) endtime
  FROM 
     ( SELECT a.* 
         FROM 
            ( SELECT name
                   , staten
                   , timeadded
                   , CASE WHEN @prev_name = name 
                          THEN CASE WHEN @prev_staten = staten 
                                    THEN @i:=@i 
                                    ELSE @i:=@i+1 END 
                          ELSE @i:=@i+1 END i
                   , @prev_name := name prev_name
                   , @prev_staten:=staten prev_state
                FROM demo
               ORDER 
                  BY name
                   , timeadded
                   , staten
            ) a
         JOIN
            ( SELECT @prev_name := null, @prev_staten := null, @i:=0) vars
     ) x
 GROUP
    BY name
     , staten
     , i
 ORDER
    BY name
     , starttime
     , staten;

+------+--------+------------+------------+
| name | staten | starttime  | endtime    |
+------+--------+------------+------------+
| ABC  |      1 | 1540250401 | 1540251601 |
| DEF  |      1 | 1540250401 | 1540251601 |
| GHI  |      1 | 1540250401 | 1540250701 |
| GHI  |      6 | 1540251001 | 1540251301 |
| GHI  |      1 | 1540251601 | 1540251601 |
| JKL  |     11 | 1540250401 | 1540251601 |
+------+--------+------------+------------+

答案 1 :(得分:-1)

更新:这是错误的答案,因为我错过了OP中的“状态更改时”部分。

SELECT
      Name
    , StateN
    , MIN(TimeAdded)
    , MAX(TimeAdded)
FROM
    demo
GROUP BY
    Name, StateN
;