The first two columns compute the differences between the corresponding end / start events. (Summation is commutative, so we do not need to actually match the corresponding events.)
The third color looks for movement start events for which the previous load event is the load start event, and for movement end events for which the next load event is the load end event.
SELECT (SELECT SUM(timestamp) FROM Table1 WHERE event = 'movement end') - (SELECT SUM(timestamp) FROM Table1 WHERE event = 'movement start') AS all_movement, (SELECT SUM(timestamp) FROM Table1 WHERE event = 'load end') - (SELECT SUM(timestamp) FROM Table1 WHERE event = 'load start') AS all_load, (SELECT SUM(timestamp) FROM Table1 a WHERE event = 'movement end' AND (SELECT event FROM Table1 b WHERE timestamp = (SELECT min(timestamp) FROM Table1 c WHERE c.timestamp >= a.timestamp AND c.event LIKE 'load %') ) = 'load end') - (SELECT SUM(timestamp) FROM Table1 a WHERE event = 'movement start' AND (SELECT event FROM Table1 b WHERE timestamp = (SELECT max(timestamp) FROM Table1 c WHERE c.timestamp <= a.timestamp AND c.event LIKE 'load %') ) = 'load start') AS load_movement;
source share