mysql - Finding intervals in a table -
The problem here is that I have state timestamps with different processes and states. for example. The process with ID 135 was at this time 1 state was "2014-01-09 15:41:22". 4 state types are 0 = fine 1 = warning 2 = important 3 = unknown. Here is a piece of table:
id state_time process_id position 37 "2014-01-09 15:41:22" 135 1 92 "2014-01-09 15:42:01" 135 0 153 "2014-01-09 15:46:28" 135 1 204 "2014-01-09 15:47:25" 135 0 25 9 "2014-02-05 14:48:00" 135 1 321 "2014 -02-05 14:49:20 "135 2 352" 2014-02-05 14:50:40 "135 2 383" 2014-02-05 14:52:00 "135 1 464" 2014-02-05 14 : 53: 20 "135 2 576" 2014-02-05 14:54:40 "135 2 621" 2014-02-05 14:56:00 "135 2 666" 2014-02-05 14:57:20 " 135 1 747 "2014-02-05 14:58:40" 135 3 792 "2014-02-05 15:00:07" 135 1 957 "2014-02-05 15:18:53" 135 0
Here I have selected only one process, but many of them can be so that you get the next row in the warning position in the first line Procedure to be able to see the process that he is cured. So this means that this warning was in the interval between 15:41:22 and 15:42:01. And again warn the state and so on So here we can find 6 warning intervals, 2 critical and 1 unknown. This work is that it calculates how long the process was in some states in some time.
What you really want is the lead ()
function, but MySQL Does not support To get next time, use a correlated subquery: Select
. *, (Select the state_time from atable t2 where t2.process_id = t.process_id and t2.state_time & gt; t.state_time and T2.state
For performance, you want an index at enabled (process_id, state_time, state)
.
The next problem is that the Times. Here's a method: Select
process_id, state, amount (seconds, statetime, next_state_time) as seconds (select t. *, (Atable T2 from state_time Choose where t2.process_id = T.process_id and t2.state_time & gt; t.state_time and t2.state
You can add the appropriate where
section to the state only for some time.
Comments
Post a Comment