例如,我有一张表,其中包含某个时间内房间内的人数
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 :(得分: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);
无法测试这个,因为我正在手机上,火车上写它。希望能帮助到你!稍后会测试。
答案 1 :(得分: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子句中的变量。
希望这会有所帮助。