This is a timeline (from timestamp 1 to 66) for commands (ON
or OFF
) that control the state of a machine (ONLNIE
or OFFLINE
).
Timeline ------------|-------------|--------|------|---------|------------>
Command ON OFF OFF ON OFF
Note the second OFF
command was useless.
Accordingly, the machine state should be:
State ---OFFLINE--|----ONLINE---|----OFFLINE----|--ONLINE-|--OFFLINE----
Note that the default state is OFFLINE
.
I have the following data for 100,000s of machines:
Machine Timestamp Command
======= ========= =======
1 13 ON
1 27 OFF
1 36 OFF
1 43 ON
1 53 OFF
...
n ... ...
I want to create the following table:
Machine From To State
======= ==== == =====
1 1 13 OFFLINE
1 13 27 ONLINE
1 27 43 OFFLINE
1 43 53 ONLINE
1 53 66 OFFLINE
...
n ... ... ...
(From
is inclusive, and To
is exclusive, thanks to @MatBailie)
What is an efficient way to do that in SQL (DB2) or R?