This is a revised version of my question.
I am currently designing a simple power quality monitoring tool. I have managed to design a MySQL database and populate the table with the voltage status and the corresponding time stamp taken at particular times. Below is my table structure.
+----+---------------------+-------------+
| 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)
Any voltage below 50V is taken as an outage. I want to write a query that queries the table for the TOTAL OUTAGE TIME between 2015-02-01 17:33:45
and 2015-02-14 00:28:57
.
Guys, kindly help me on this because I really don't know how to approach it.
This should work:
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';
I have assumed that a voltage of 50 is also considered as an outage and I have assigned the table name as voltage.
The first step to properly address this kind of queries is to join every row with its previous row.
SELECT *
FROM voltage AS V
LEFT JOIN voltage AS PRV_V ON (PRV_V.Id = V.Id-1)
Now you just have to keep those rows which its previous measure was below 50 and sum their timestamp differences.
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'
EDIT: If your data don't have autoincremental ids or you have gaps between your ids you can still join your measurements with its previous measurement using the timestamp:
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
)
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.