sql查询每15分钟平均一次能量

时间:2014-10-29 07:42:02

标签: mysql

选项(我的表)

id      datetime           energy   
1   2014-10-28 04:00:00     14     
1   2014-10-28 04:05:00     16
1   2014-10-28 04:10:00     23
1   2014-10-28 04:15:00     45
1   2014-10-29 04:00:00     34
1   2014-10-29 04:05:00     33
1   2014-10-29 04:10:00     12
1   2014-10-29 04:15:00     67

输出

id    datetime 
1     2014-10-28 04:15:00    28
1     2014-10-29 04:15:00    37.33

我的疑问:

SELECT date(`datetime`) dateDay,id, 
15*floor(date_format(`datetime`,'%i')/15) dateHour, 
avg(energy) FROM `meter` 
WHERE `datetime` >= '2014-10-28 00:00:01' AND `datetime` <= '2014-10-29 23:59:59' 
GROUP BY id,day(datetime),month(datetime),dateHour

1 个答案:

答案 0 :(得分:-1)

如果我理解,您希望以15分钟为间隔进行分组。如果是这样,你会得到类似的东西:

SELECT
    id,
    avg(energy) as average_value,
    date_format(datetime, "%Y-m-d") as date_day,
    date_format(datetime,'%H') as date_hour
    IF(date_format(datetime,'%i') < 15, 0,
    IF(date_format(datetime,'%i') <= 30, 15,
    IF(date_format(datetime,'%i') <= 45, 30,45))) as fifteen_minutes_slot
from deliverydestination
GROUP BY id, date_day, date_hour, fifteen_minutes_slot

1)使用一组ifs计算十五分钟的时隙:如果分钟的值小于15,则给它值0,否则如果它小于30,则给它值15,等等...... / p>

2)按天,小时和15分钟分组值。并且通过id,如在您的示例中,如果需要。

最好不要弄清楚数据,以显示最终结果......