Checking consecutive values at a MySQL query -
i have mysql table this:
id - time - value
and i'm getting every pair of id, time (grouped id) value greater threshold. basicaly, i'm getting every id has @ least 1 time value greater threshold. query looks this:
select id, time mydb.mytable value>%s , time>=%s , time<=%s group id
edit: time checks allow operate in time range of choice between data table; has nothing else asking.
it works perfectly, want add filtering: want avoid times value greater threshold (let's call alarms) if alarm hasn't happened time before or after. mean: if alarm accurs @ single, isolated instant of time instead of 2 consecutive instants of time, i'll consider false alarm , avoid returned @ query response.
of course can call each id check this, i'd in single query make faster. guess use conditionals, don't have expertise @ mysql.
any help?
edit2: example threshold = 10
id - time - value 1 - 2004 - 9 1 - 2005 - 11 1 - 2006 - 8 2 - 2107 - 12 2 - 2109 - 13 3 - 3402 - 11 3 - 3403 - 12
in example, id 3 should valid alarm, since 2 consecutive time values id have value > threshold. id 1 has single, isolated alarm, should filteres. id 2 there 2 alarms, not consecutive, should filtered.
something this:
10 - threshold 0 - minimum of time period 100000 - maximum of time period select id, min(time) ( select id, time, (select max(time) t time<t1.time , id=t1.id , value>10) lag_g, (select max(time) t time<t1.time , id=t1.id , value<=10) lag_l, (select min(time) t time>t1.time , id=t1.id , value>10) lead_g, (select min(time) t time>t1.time , id=t1.id , value<=10) lead_l t t1 value>10 , time>=0 , time<=100000 ) t3 ifnull(lag_g,0)>ifnull(lag_l,0) or ifnull(lead_g,100000)<ifnull(lead_l,100000) group id
this query works searching near records.
if need search records time (+1, -1 ) you've mentioned in comment try query:
select id, min(time) t t1 value>10 , time>=%s2 , time<=%s1 , ( exists(select 1 t value>10 , id=t1.id , time=t1.time-1) or exists(select 1 t value>10 , id=t1.id , time=t1.time+1) ) group id
Comments
Post a Comment