2012-07-13 68 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