每分钟我的数据库中都有一个值。每条记录都有一个时间戳和一个值。
"30695","2015-09-06 18:10:09","693"
我想获得一周中每一天的每分钟的平均值,但我不确定如何为此构建查询。
我的想法是,我首先需要按DATEPART(weekday,[time])
对所有记录进行分组,这会将我的所有数据放入星期日,星期一,星期二等。接下来,我会按分钟分组,舍入。 (所以2015/11/30 06:41: 28 将与2015/11/23 06:41分组: 56 (两个日期都是星期一))。然后,取每分钟的平均值 - 7 * 24 * 60 = 10,080总值。
答案 0 :(得分:1)
mysql中没有datepart()函数,但您可以使用dayofweek()获取星期几,minute()获取分钟部分:
select dayofweek(timestamp) as day, minute(timestamp) as minute, avg(value_field) as average
from table
group by dayofweek(timestamp), minute(timestamp);
答案 1 :(得分:0)
让它工作,改编自Shadow的答案,这是不对的。这是我的查询(MySQL 5.5)
SELECT weekday(timestamp) as day_of_week, avg(avg) as average, DATE_FORMAT(timestamp,'%H:%i') as hour_time
FROM audio
GOUP BY hour_time, day_of_week
ORDER BY `day_of_week` ASC