....
09.05.11 12:10 | 14,5
09.05.11 12:00 | 19,5
09.05.11 11:50 | 13,9
09.05.11 11:40 | 15,4
09.05.11 11:30 | 15,6
09.05.11 11:20 | 14,3
09.05.11 11:10 | 16,7
09.05.11 11:00 | 19,3
09.05.11 10:50 | 12,6
09.05.11 10:40 | 10,8
09.05.11 10:30 | 10,5
09.05.11 10:20 | 15,6
09.05.11 10:10 | 14,9
09.05.11 09:00 | 10,1
09.05.11 09:50 | 10,7
....
我每小时需要AVG
个数据。
离。 09.05.11 11.00
的平均数据是根据(09.05.11 10.10
到09.05.11 11.00
)计算的。
12.00
的 11.10 - 12.00
等等......
我该怎么做?
答案 0 :(得分:5)
TRY
SELECT AVG( visit ) , HOUR( `time_column` )
FROM time_table
WHERE DATE_SUB( `time_column` , INTERVAL 1 HOUR )
GROUP BY HOUR( `time_column` )
CREATE TABLE IF NOT EXISTS `time_table` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`waqt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`visit` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;
--
-- Dumping data for table `time_table`
--
INSERT INTO `time_table` (`id`, `waqt`, `visit`) VALUES
(1, '2011-07-28 13:29:04', 3),
(2, '2011-07-28 15:29:10', 4),
(3, '2011-07-28 13:45:35', 7),
(4, '2011-07-28 15:00:47', 5),
(5, '2011-07-28 14:45:03', 6),
(6, '2011-07-28 13:00:21', 3);
然后我每小时访问
SELECT AVG(visit), HOUR(waqt)
FROM time_table
WHERE DATE_SUB(`waqt`,INTERVAL 1 HOUR) GROUP BY HOUR(waqt)