Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL-平均间隔15分钟_Mysql_Sql - Fatal编程技术网

MySQL-平均间隔15分钟

MySQL-平均间隔15分钟,mysql,sql,Mysql,Sql,我有下表: ╔════════════╦══════════╦═══════╦══════╗ ║ DATE ║ TIME ║ Price ║ Avg ║ ╠════════════╬══════════╬═══════╬══════╣ ║ 01/01/2000 ║ 00:00:00 ║ 1 ║ ║ ║ 01/01/2000 ║ 00:05:00 ║ 2 ║ ║ ║ 01/01/2000 ║ 00:10:00 ║ 3 ║

我有下表:

╔════════════╦══════════╦═══════╦══════╗
║    DATE    ║   TIME   ║ Price ║ Avg  ║
╠════════════╬══════════╬═══════╬══════╣
║ 01/01/2000 ║ 00:00:00 ║   1   ║      ║
║ 01/01/2000 ║ 00:05:00 ║   2   ║      ║
║ 01/01/2000 ║ 00:10:00 ║   3   ║      ║
║ 01/01/2000 ║ 00:15:00 ║   4   ║      ║
║ 01/01/2000 ║ 00:20:00 ║   5   ║      ║
║ 01/01/2000 ║ 00:25:00 ║   6   ║      ║
║ 01/01/2000 ║ 00:30:00 ║   7   ║      ║
║ 01/01/2000 ║ 00:35:00 ║   8   ║      ║
║ 01/01/2000 ║ 00:40:00 ║   9   ║      ║
║ 01/01/2000 ║ 00:45:00 ║   10  ║      ║
║ 01/01/2000 ║ 00:50:00 ║   11  ║      ║
║ 01/01/2000 ║ 00:55:00 ║   12  ║      ║
║ 01/01/2000 ║ 01:00:00 ║   13  ║      ║
╚════════════╩══════════╩═══════╩══════╝
我想用过去15分钟价格的移动平均值(不包括最后的平均值)填充Avg列,结果是:

╔════╦════════════╦══════════╦═══════╦══════╗
║ ID ║    DATE    ║   TIME   ║ Price ║ Avg  ║
╠════╬════════════╬══════════╬═══════╬══════╣
║ 1  ║ 01/01/2000 ║ 00:00:00 ║   10  ║  10  ║
║ 2  ║ 01/01/2000 ║ 00:05:00 ║   2   ║      ║
║ 3  ║ 01/01/2000 ║ 00:10:00 ║   6   ║      ║
║ 4  ║ 01/01/2000 ║ 00:15:00 ║   4   ║  4   ║ <-- Average of 2,6,4
║ 5  ║ 01/01/2000 ║ 00:20:00 ║   7   ║      ║
║ 6  ║ 01/01/2000 ║ 00:25:00 ║   6   ║      ║
║ 7  ║ 01/01/2000 ║ 00:30:00 ║   5   ║  6   ║ <-- Average of 7,6,5
║ 8  ║ 01/01/2000 ║ 00:35:00 ║   2   ║      ║
║ 9  ║ 01/01/2000 ║ 00:40:00 ║   2   ║      ║
║ 10 ║ 01/01/2000 ║ 00:45:00 ║   2   ║  2   ║ <-- Average of 2,2,2
║ 11 ║ 01/01/2000 ║ 00:50:00 ║   10  ║      ║
║ 12 ║ 01/01/2000 ║ 00:55:00 ║   12  ║      ║
║ 13 ║ 01/01/2000 ║ 01:00:00 ║   2   ║  8   ║ <-- Average of 10,12,2
╚════╩════════════╩══════════╩═══════╩══════╝
但这似乎是对15分钟和下一分钟的参赛作品进行分组,而不是像我想的那样对之前的作品进行分组

非常感谢您的帮助


谢谢

您可以使用相关子查询执行任何操作:

select t.*,
       (select avg(t2.price)
        from table t2
        where t2.time <= t.time and t2.time >= date_sub(t.time, interval 15 minute)
       ) as avgprice
from table t;

可以使用相关子查询执行所需操作:

select t.*,
       (select avg(t2.price)
        from table t2
        where t2.time <= t.time and t2.time >= date_sub(t.time, interval 15 minute)
       ) as avgprice
from table t;

您的
时间
栏的类型是什么?您是否将日期/时间数据存储为varchar?否,它是time类型您的
time
列的类型是什么?您是否将日期/时间数据存储为varchar?不,时间类型非常有效,谢谢!虽然我在这个表中有几百万条记录,但是更新整个表需要大约一个小时的处理。是否可以进行任何优化?按时添加索引怎么样?@VincentLavoie。
(时间、价格)
上的索引可能有助于提高性能。这非常有效,谢谢!虽然我在这个表中有几百万条记录,但是更新整个表需要大约一个小时的处理。是否可以进行任何优化?按时添加索引怎么样?@VincentLavoie。
(时间、价格)
上的索引可能有助于提高性能。
update table t join
       (select t.*,
               (select avg(t2.price)
                from table t2
                where t2.time <= t.time and t2.time >= date_sub(t.time, interval 15 minute)
               ) as avgprice
        from table t
       ) tt
       on t.id = tt.id
    set t.15_MIN_AVERAGE = ttavgprice
    where minute(t.time) in (0, 15, 30, 45);