I have a table which holds the status of a machine in our factory. When the machine is running a line is written with a status of 1, when it stops it writes a status of 5 until a program records the error then it writes a line with a status of 6 until the machine is running again when it writes a 1 and so on
STATUS
| Status | Error | Timestamp |
| 1 | | 2010-07-27 08:26:12 | -Machine is running
| 5 | 22 | 2010-07-27 08:29:15 | -Machine has stopped with an error code of 22
| 6 | | 2010-07-27 08:29:16 | -Error has been logged, machine still down
| 3 | | 2010-07-27 08:30:48 | -unrelated status event, machine still down
| 1 | | 2010-07-27 08:31:33 | -Machine is running again
I would like to query this to have an outcome of:
| Time Down | Error | Timestamp |
| 00:02:18 | 22 | 2010-07-27 08:26:15 |
And, if possible, change the error '22' to the actual error reason which is stored in a separate table:
| Time Down | Error | Timestamp |
| 00:05:18 | Spout Jam | 2010-07-27 08:26:15 |
I have a separate table with error codes and reasons,
ALARMS
| Error_code | Reason |
| 1 | Over Temperature |
| 22 | Spout Jam |
So it needs to get the timestamp with a status 5 and subtract the timestamp with a status 1 before the 5 came up. :confused: yeah, that made it sound easier.