2013-10-26 17 views
0

我想要一個查詢來查找小於某個值的數據,並且大於某些值,並且時間戳間隔爲5分鐘。在mysql中選擇帶time_stamp差異的查詢

例如20.4是< = 26.0和25.5也< = 26.0但TIME_STAMP值diffrence僅3分鐘,所以我想選擇20.4,40.1,22.3,32.2

20.4 October, 26 2013 13:12:22+0000 
25.5 October, 26 2013 13:15:22+0000 
40.1 October, 26 2013 13:35:22+0000 
22.3 October, 26 2013 13:43:22+0000 
19.78 October, 26 2013 13:45:22+0000 
32.2 October, 26 2013 13:51:22+0000 

但我想結果集像,

20.4 October, 26 2013 13:12:22+0000 
40.1 October, 26 2013 13:35:22+0000 
22.3 October, 26 2013 13:43:22+0000 
32.2 October, 26 2013 13:51:22+0000 

請找SQL小提琴link

回答

0

試試這個:

SELECT type,time_stamp 
FROM (
    select type,time_stamp, 
      if(time_stamp > @prev_tm + interval 5 minute, 
      (@prev_tm:=time_stamp), @prev_tm) prev_tm 
    from table1, 
    (
     SELECT (@prev_tm:= min(time_stamp) - interval 999 minute) 
     FROM table1 
    ) init_vars 
    where time_stamp >= '2013-10-26 13:12:22' 
    and (type <= 26 or type >= 30) 
    order by time_stamp 
) sub_query 
WHERE time_stamp = prev_tm 

演示:http://www.sqlfiddle.com/#!2/e67b3/24