【问题标题】:Mysql average value every minute refer to unix timestampMysql每分钟平均值参考unix时间戳
【发布时间】:2012-10-24 05:56:47
【问题描述】:

我用 unix 时间戳每 1-3 秒记录一次我的温度,我希望平均每 1 分钟返回一行数据,并持续 24 小时。

我的记录器表如下所示:

unixtime    temp
1350899052  25.37
1350899054  25.44
1350899057  25.44
1350899059  25.44
1350899062  25.44
1350899064  25.44
1350899069  25.44
1350899071  25.44

我希望它返回

unixtime    temp
1350899052  25.37 -- average value of 1 minute range both unixtime and temp 
1350899054  25.44
1350899057  25.44

请告知我应该如何执行 mySQL 命令?谢谢。

【问题讨论】:

    标签: mysql average unix-timestamp


    【解决方案1】:

    以下应该有效:

    SELECT ROUND(unixtime/(60)) AS minute, avg(temp) FROM table where unixtime >= UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 DAY)) GROUP BY ROUND(unixtime/(60));

    以正常格式显示日期:

    SELECT from_unixtime(floor(unixtime/(60))*60) AS minute, avg(temp) FROM test where unixtime >= UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 DAY)) GROUP BY floor(unixtime/(60))

    输出:

    minute  avg(temp)
    2012-10-24 08:02:00 37.5
    2012-10-24 08:03:00 45
    

    【讨论】:

    • 是的,因为它显示的是 1970 年的分钟数。您的输出应该是什么样的? 2012-10-12 00:00:01 25,777 2012-10-12 00:00:02 28,999 ?
    • 我得到了 munite 的回报:22517598 26.64000075204032 不是我预期的 unix 时间平均值,但接近我如何在平均时间段内获得 unix 时间的回报,感谢您的帮助
    • 你能解释一下 GROUP BY ROUND(unixtime/(60));我不明白它是如何工作的。再次感谢。
    【解决方案2】:
     1. 1 min
    
    select * from logger_table where unixtime> timestamp(DATE_SUB(NOW(), INTERVAL 1 MINUTE)) group by temp
    
     2. 24 hrs
    select * from logger_table where unixtime> timestamp(DATE_SUB(NOW(), INTERVAL 24 HOUR)) group by temp;
    

    【讨论】:

    • 这1分钟内可能没有数据
    猜你喜欢
    • 1970-01-01
    • 2020-10-07
    • 2019-02-21
    • 2021-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-02
    • 1970-01-01
    相关资源
    最近更新 更多