2012-07-13 67 views
0

比如我有了的人在一个房间的数量在给定时间平均每10分钟的数据,并删除其余

room_color people time 
red   100  2012-07-13 11:11:11 
red   120  2012-07-13 11:14:12 
red   130  2012-07-13 11:17:13 
green   ... etc 

如何找到的人在平均量的表在10分钟间隔的房间,例如总结在红房间在11:10人的数量是〜117

我知道一些方法来做到这一点,但它都涉及到创建一个新的表,如何做我这样做而不创建一个新表?我只是想删除不相关的数据。

+0

我正在保留数据源,但我需要此表的数据较少。 – Julian 2012-07-13 18:26:28

回答

0
Insert into room (room_color, people) 
(Select color, avg(people) as people from room 
where time between '2012-07-13 11:10' and '2012-07-13 11:20' 
Group by room_color) 

Delete From room where time between '2012-07-13 11:10' and '2012-07-13 11:20' 

-- in case of need set some time to the table 
update room set time = '2012-07-13 11:10' Where time is null 

试试这个,如果你想要的话你可以使用两个日期时间变量并加一个10分钟。使用between子句中的变量。

希望这有助于。

+0

谢谢,但我想要改变桌子的东西。 – Julian 2012-07-13 18:30:23

+0

更新后的解决方案应该为您提供一个起点。希望能帮助到你。 – 2012-07-13 18:40:33

0
INSERT INTO roomstats 
SELECT room_color,AVG(people), '2012-07-13 11:10:00' 
FROM roomstats 
WHERE room_color = 'red' 
    AND DATE(time) = CAST('2012-07-13' AS DATE) 
    AND TIME(time) >= CAST('11:10:00' AS TIME) 
    AND TIME(time) <= CAST('11:20:00' AS TIME); 

无法测试这个,因为我正在将它写在我的手机上,在火车上。希望能帮助到你!稍后再测试。

+0

谢谢,但我想要改变桌子的东西。 – Julian 2012-07-13 18:37:49