SQL query to obtain an Average value for set periods of time

我们两清 提交于 2019-12-04 09:45:54

Here is your query:

SELECT STR_TO_DATE(CONCAT(DATE_FORMAT(`time`, '%H'), ':', (FLOOR(DATE_FORMAT(`time`, '%i') / 15) * 15), ':00'), '%H:%i:%s') `starttime`, AVG(`power`) `avgpower`
FROM `tablea`
GROUP BY `starttime`;

Please feel free to replace the table (tablea) and columns (time and power) names according to your schema.

Hope this helps.

You can probably group by the rounded value of minutes/4 and then select the average of the power reading field. You might prefer using the floor function over rounding the number of minutes.

user2186808

Had the same issue. As Don Kirkby pointed out, I solved it with this code:

SELECT time, AVG(power) AS avgpower,  ROUND(UNIX_TIMESTAMP(time)/(15*60)) AS timekey
FROM table
GROUP BY timekey
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!