I have been struggling with this question for a couple of days. I have one machine which can have errors. In a database I have the starting and ending time (in unix time) when the error appeared, and the type of error (number from 5-12). The problem I'm having is that multiple errors can occur at the same time (and overlap).
My table looks like this:
id| type | from | to
1| 6 | 1417179933 | 1417180006
2| 6 | 1417180035 | 1417180065
3| 9 | 1417180304 | 1417180409
4| 6 | 1417180662 | 1417184364
5| 8 | 1417180662 | 1417186832
6| 9 | 1417180662 | 1417184364
7| 12 | 1417180662 | 1417184364
8| 6 | 1417184364 | 1417186832
9| 9 | 1417184364 | 1417188054
I need to find the total duration of errors for this machine. I can't sum all differences from the above table since it's possible that two or more errors appeared in the same time interval. Records are sorted in ascending order.
My guess was to compare each record (start and finishing time) with previous and then find the difference in seconds. However, this table can grow over time and it is a problem to search through it.
Is there a clever way in PHP or MySQL to find total time where the machine didn't work, possibly in minutes?