gpt4 book ai didi

php - 如何获得每分钟存储的值的每小时平均值

转载 作者:行者123 更新时间:2023-11-28 23:21:36 27 4
gpt4 key购买 nike

下表(几乎)每分钟通过 cronjob 接收一个数据集:

id  |  timestamp  | humanTime          | userCount  
-----------------------------------------------------
1 1482310202 2016-12-21 09:50:07 120
2 1482310262 2016-12-21 09:51:07 126
3 1482310322 2016-12-21 09:52:06 110
4 1482310381 2016-12-21 09:54:06 131
5 ...

由于 cronjob 是通过网络查询,因此由于可能超时,无法确保每小时有 60 个条目。

我想计算过去 X 小时内 userCount 列的每小时平均值。

有没有人有想法?

P.S.: 如果没有办法通过sql来做到这一点,我想通过PHP来解决这个问题

最佳答案

您只需要从时间中提取日期和小时。我会坚持人类时间:

select date(humanTime) as dd, hour(humanTime) as hh, avg(userCount)
from t
group by date(humanTime), hour(humanTime)
order by dd, hh;

如果您想要最后“n”个小时,则包括:

where humanTime >= date_sub(now(), interval "n" hour)

关于php - 如何获得每分钟存储的值的每小时平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41360185/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com