用于计算两个时间戳之间的断电的Mysql查询

时间:2015-02-28 06:50:01

标签: php mysql

这是我的问题的修订版。

我目前正在设计一种简单的电能质量监测工具。我设法设计了一个MySQL数据库,并在表格中填写电压状态和在特定时间采取的相应时间戳。下面是我的表结构。

+----+---------------------+-------------+
| Id | Time_Stamp          | Red_Ph_Volt |
+----+---------------------+-------------+
|  1 | 2015-02-01 17:33:45 |      250.00 |
|  2 | 2015-02-01 18:53:41 |      250.00 |
|  3 | 2015-02-01 18:54:39 |       25.00 |
|  4 | 2015-02-01 18:54:54 |      242.00 |
|  5 | 2015-02-01 18:55:11 |      222.00 |
|  6 | 2015-02-02 21:00:29 |      250.00 |
|  7 | 2015-02-02 21:00:45 |      220.00 |
|  8 | 2015-02-02 21:00:55 |      230.00 |
|  9 | 2015-02-02 21:03:01 |      230.00 |
| 10 | 2015-02-02 21:03:36 |      250.00 |
| 11 | 2015-02-02 21:03:46 |       50.00 |
| 12 | 2015-02-06 17:54:08 |        0.00 |
| 13 | 2015-02-06 23:04:04 |      220.00 |
| 14 | 2015-02-06 23:04:34 |      220.00 |
| 15 | 2015-02-06 23:05:51 |      250.00 |
| 16 | 2015-02-08 16:04:44 |      220.00 |
| 17 | 2015-02-08 16:06:29 |      220.00 |
| 18 | 2015-02-09 09:04:12 |      220.00 |
| 19 | 2015-02-09 10:42:39 |      203.00 |
| 20 | 2015-02-09 19:34:43 |      203.00 |
| 21 | 2015-02-09 21:57:02 |      203.00 |
| 22 | 2015-02-10 09:47:08 |        0.00 |
| 23 | 2015-02-10 11:15:34 |      250.00 |
| 24 | 2015-02-10 11:48:14 |      250.00 |
| 25 | 2015-02-10 13:18:14 |      220.00 |
| 26 | 2015-02-10 18:59:52 |        0.00 |
| 27 | 2015-02-10 22:44:14 |      250.00 |
| 28 | 2015-02-10 22:47:10 |      212.00 |
| 29 | 2015-02-14 00:02:10 |      212.00 |
| 30 | 2015-02-14 00:28:57 |      242.00 |
| 31 | 2015-02-14 00:35:56 |       21.00 |
| 32 | 2015-02-16 12:11:47 |       21.00 |
+----+---------------------+-------------+
32 rows in set (0.02 sec)

任何低于50V的电压都被视为中断。我想编写一个查询表格,查询2015-02-01 17:33:452015-02-14 00:28:57之间的总停机时间

伙计们,请帮助我,因为我真的不知道如何接近它。

2 个答案:

答案 0 :(得分:0)

这应该有效:

SELECT sum(timestampdiff(SECOND, Time_Stamp, (SELECT Time_Stamp
                                              FROM voltage AS V2
                                              WHERE V2.Id > V1.Id
                                              LIMIT 1))) AS T
FROM voltage AS V1
WHERE Red_Ph_Volt <= 50 AND Time_Stamp BETWEEN '2015-02-01 17:33:45' AND '2015-02-14 00:28:57';

我假设50的电压也被认为是中断,我已将表名指定为电压。

答案 1 :(得分:0)

正确处理此类查询的第一步是将每一行与其前一行连接。

   SELECT *
     FROM voltage AS V
LEFT JOIN voltage AS PRV_V ON (PRV_V.Id = V.Id-1)

现在你只需要保留上一个度量低于50的那些行,并总结它们的时间戳差异。

   SELECT SUM(TIMESTAMPDIFF(SECOND, PRV_V.Time_Stamp,  V.Time_Stamp))
     FROM voltage AS V
LEFT JOIN voltage AS PRV_V ON (PRV_V.Id = V.Id-1)
    WHERE PRV_V.Red_Ph_Volt < 50
      AND V.Time_Stamp BETWEEN '2015-02-01 17:33:45' AND '2015-02-14 00:28:57'

编辑:如果您的数据没有自动增量ID,或者您的ID之间存在间隙,您仍然可以使用时间戳将测量值与之前的测量值结合起来:

   SELECT *
     FROM voltage AS V
LEFT JOIN voltage AS PRV_V 
       ON (  PRV_V.Time_Stamp < V.Time_Stamp
             AND NOT EXISTS ( SELECT *  
                                FROM voltage 
                               WHERE Time_Stamp > PRV_V.Time_Stamp
                                 AND voltage.Time_Stamp < V.Time_Stamp
                            )
          )