I am required to find out the loss duration when the machine is running but not running any load. The machine is able to run several load concurrently which creates a lot of overlapping datetime.
Gantt Chart illustration of my task:
Sample Table Structure
+-----------+------+-------+---------------------+---------------------+-----------+
| Equipment | Type | State | Tracked_inTIME | Tracked_outTIME | Load_Type |
+-----------+------+-------+---------------------+---------------------+-----------+
| A1 | MACH | UP | 2018-11-13 08:00:00 | 2018-11-13 10:00:00 | NULL |
| A1 | LOAD | NULL | 2018-11-13 08:20:00 | 2018-11-13 09:43:00 | PROD |
| A1 | LOAD | NULL | 2018-11-13 08:22:30 | 2018-11-13 09:55:00 | PROD |
| A1 | LOAD | NULL | 2018-11-13 08:30:00 | 2018-11-13 10:11:00 | PROD |
| A1 | MACH | DOWN | 2018-11-13 10:00:00 | 2018-11-13 10:35:00 | NULL |
| A1 | MACH | UP | 2018-11-13 10:35:00 | 2018-11-13 15:11:00 | NULL |
| A1 | LOAD | NULL | 2018-11-13 11:00:00 | 2018-11-13 11:51:00 | PROD |
| A1 | LOAD | NULL | 2018-11-13 11:25:00 | 2018-11-13 11:55:00 | PROD |
| A1 | LOAD | NULL | 2018-11-13 12:05:00 | 2018-11-13 14:11:00 | PROD |
| A1 | LOAD | NULL | 2018-11-13 12:25:00 | 2018-11-13 13:05:00 | PROD |
| A1 | LOAD | NULL | 2018-11-13 13:45:00 | 2018-11-13 15:11:00 | PROD |
| A2 | MACH | UP | 2018-11-13 08:00:00 | 2018-11-13 14:05:00 | NULL |
| A2 | LOAD | NULL | 2018-11-13 08:00:00 | 2018-11-13 14:05:00 | PROD |
+-----------+------+-------+---------------------+---------------------+-----------+
Wrong Output (Column: Gapduration = 40 , should be 0 as the machine is still running a load)
+----+-------+---------------------+---------------------+---------------------+----------------------+-------------+
| EQ | State | EQ_inTIME | EQ_outTIME | LOAD_inTIME | LOAD_outTIME | GapDuration |
+----+-------+---------------------+---------------------+---------------------+----------------------+-------------+
| A1 | UP | 2018-11-13 08:00:00 | 2018-11-13 10:00:00 | 2018-11-13 08:20:00 | 2018-11-13 09:43:00 | 20 |
| A1 | UP | 2018-11-13 08:00:00 | 2018-11-13 10:00:00 | 2018-11-13 08:22:30 | 2018-11-13 09:55:00 | 0 |
| A1 | UP | 2018-11-13 08:00:00 | 2018-11-13 10:00:00 | 2018-11-13 08:30:00 | 2018-11-13 10:11:00 | 0 |
| A1 | DOWN | 2018-11-13 10:00:00 | 2018-11-13 10:35:00 | 2018-11-13 08:30:00 | 2018-11-13 10:11:00 | 0 |
| A1 | UP | 2018-11-13 10:35:00 | 2018-11-13 15:11:00 | 2018-11-13 11:00:00 | 2018-11-13 11:51:00 | 25 |
| A1 | UP | 2018-11-13 10:35:00 | 2018-11-13 15:11:00 | 2018-11-13 11:25:00 | 2018-11-13 11:55:00 | 0 |
| A1 | UP | 2018-11-13 10:35:00 | 2018-11-13 15:11:00 | 2018-11-13 12:05:00 | *2018-11-13 14:11:00 | 10 |
| A1 | UP | 2018-11-13 10:35:00 | 2018-11-13 15:11:00 | 2018-11-13 12:25:00 | 2018-11-13 13:05:00 | 0 |
| A1 | UP | 2018-11-13 10:35:00 | 2018-11-13 15:11:00 | 2018-11-13 13:45:00 | 2018-11-13 15:11:00 | 40 |
| A2 | UP | 2018-11-13 08:00:00 | 2018-11-13 14:05:00 | 2018-11-13 08:00:00 | 2018-11-13 14:05:00 | 0 |
+----+-------+---------------------+---------------------+---------------------+----------------------+-------------+
- Output was derived by doing a self join to bind the load time frame to equipment state.
- The load_inTIME at the first row of every equipment state change is compared with the equipment_inTIME
Subsequent rows of load_inTIME are compared to the previous load_outTIME using the lag() function
The main problem that I had was that there was a load timeframe (marked with *) which spans over multiple load duration that my calculation did not manage to account for
Any suggestions?
