dongqing220586 2013-06-14 13:16
浏览 100

检测符合时间序列中特定条件的连续项目

I have a MySQL data base with more than 92.000 rows with weather registers every half an hour. Day | Month | Year | Time | Temperature |... I'm trying to obtain (in PHP) Peak temperatures: Show the maximum amount of time(consecutive registers) with Temperature =< min(temperature)+3 in each month.

I would appreciate any help!

  • 写回答

1条回答 默认 最新

  • duan5362 2013-06-17 02:31
    关注

    My approach to this: start with the time-series of observations, and give each one a serial number.

    This serial numbering is a pain in the neck in MySQL, but no matter. Given a table with a ts column (a datetime item) and a temp column, here's the query to get them with serial numbers.

    SELECT @sample:=@sample+1 AS ser, ts, temp
      FROM (
         SELECT ts,temp
           FROM t
          ORDER BY ts
        ) C,
      (SELECT @sample:=0) s 
    

    Take a look at this sqlfiddle: http://sqlfiddle.com/#!2/d81e2/5/0

    OK, that's pretty trivial. Now, let's say we're looking for periods of time where the temperature is 25 degrees or above. To do this we need to chop up the time series so it omits those observations. That goes like this:

    SELECT @sample:=@sample+1 AS ser, ts, temp
      FROM (
         SELECT ts,temp
           FROM t
          WHERE NOT temp >= 25
          ORDER BY ts
        ) C,
      (SELECT @sample:=0) s
    

    Here's the sqlfiddle: http://sqlfiddle.com/#!2/d81e2/6/0

    Now the next trick is to find the time gaps in this sequence. We can use the technique from this SO post to do that. Method of finding gaps in time series data in MySQL?

    Next step, we join it to itself.

    SELECT two.ser, two.ts, two.temp, 
           TIMESTAMPDIFF(MINUTE, two.ts, one.ts) gap
      FROM (
         /* virtual table */
      ) ONE
      JOIN (
         /* same virtual table */
      ) TWO ON (TWO.ser+ 1 = ONE.ser)
    

    This query gets the time gap between each item in the series and the one after it. It's a straightforward thing to do conceptually, but tricky in the MySQL version of SQL. Here's the full query.

    SELECT two.ser, two.ts, two.temp, 
           TIMESTAMPDIFF(MINUTE, two.ts, one.ts) gap
          FROM (
     SELECT @sample:=@sample+1 AS ser, ts, temp
      FROM (
         SELECT ts,temp
           FROM t
          WHERE NOT temp >= 25
          ORDER BY ts
        ) C,
      (SELECT @sample:=0) s
          ) ONE
          JOIN (
    SELECT @sample2:=@sample2+1 AS ser, ts, temp
      FROM (
         SELECT ts,temp
           FROM t
          WHERE NOT temp >= 25
          ORDER BY ts
        ) C,
      (SELECT @sample2:=0) s
          ) TWO ON (TWO.ser+ 1 = ONE.ser)
    

    Here's the sqlfiddle: http://sqlfiddle.com/#!2/d81e2/13/0 Notice that some of the gaps are 30 minutes in duration. That's normal for consecutive readings. Some are 60 minutes. That's also normal, because the time series I'm using has some missing entries. The entries in this result set show the times and temperatures immediately before the gaps.

    So, all that's left is to get rid of the junk gaps (30 and 60 minutes) and then order the remaining gaps in descending order.

    SELECT two.ts, two.temp, 
           TIMESTAMPDIFF(MINUTE, two.ts, one.ts) gap
          FROM (
     SELECT @sample:=@sample+1 AS ser, ts, temp
      FROM (
         SELECT ts,temp
           FROM t
          WHERE NOT temp >= 25
          ORDER BY ts
        ) C,
      (SELECT @sample:=0) s
          ) ONE
          JOIN (
    SELECT @sample2:=@sample2+1 AS ser, ts, temp
      FROM (
         SELECT ts,temp
           FROM t
          WHERE NOT temp >= 25
          ORDER BY ts
        ) C,
      (SELECT @sample2:=0) s
          ) TWO ON (TWO.ser+ 1 = ONE.ser)
     WHERE TIMESTAMPDIFF(MINUTE, two.ts, one.ts)> 60
     ORDER BY TIMESTAMPDIFF(MINUTE, two.ts, one.ts) DESC
    

    This gives one row for each sequence of time where the temperature is above 25 degrees; the longest time first. The item shown in the result set is the last time temperature below 25 before it went up. SQL Fiddle. http://sqlfiddle.com/#!2/d81e2/14/0

    Fun, eh?

    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题