I'm looking for a way to get the total time between two alternating events in MYSQL.
I'm using a MYSQL database to store events when a door opens/closes. I'm looking for a way to visualise the total time a door was open each hour.
I'm using datareportive to visualize my data, currently I'm able to show how many times the door opened each hour by the query below:
SELECT DATE_FORMAT(DATE_ADD(date, INTERVAL 30 MINUTE),'%Y-%m-%d %H:00:00') as hour,
COUNT(*) as num_rows
FROM events
GROUP BY hour;
The database looks like:
ID door Action date
13 my unique door name! Open 2019-09-09T09:01:12
14 my unique door name! Close 2019-09-09T09:01:23
15 my unique door name! Open 2019-09-09T09:01:30
16 my unique door name! Close 2019-09-09T09:01:35
17 my unique door name! Open 2019-09-09T09:01:37
18 my unique door name! Close 2019-09-09T09:01:40
19 my unique door name! Open 2019-09-09T09:01:50
I'm looking for a way the query returns the total time the door was open each hour.
There is one big catch: sometimes the events Open/Close don't alternate correctly, for example power outage on the doorsensor could result in two times the same event listed in the database.