MySQL:从特定日期获取记录,并每小时对特定站点进行分组

时间:2012-07-11 08:06:32

标签: mysql sql mysqli

CREATE TABLE `call_log` (
  `call_id` int(11) NOT NULL AUTO_INCREMENT,
  `call_time` datetime NOT NULL,
  `caller` varchar(32) NOT NULL,
  `name_recording_url` varchar(123) DEFAULT NULL,
  `phone` varchar(10) NOT NULL,
  `forwarded_from` varchar(16) NOT NULL,
  `called_number` varchar(16) DEFAULT NULL,
  `sent_to_phone` varchar(10) NOT NULL,
  `market` varchar(64) NOT NULL,
  `station` varchar(12) NOT NULL,
  `distributor_id` int(11) NOT NULL,
  `distributor_phone` varchar(10) NOT NULL,
  `answered` enum('Yes','No') NOT NULL DEFAULT 'No',
  `mode` enum('Live','Test') NOT NULL,
  PRIMARY KEY (`call_id`)
)

所以,我想在不同的时间内通过总电话来填充电台。

例如,我们有车站ABCD和ASDF。 ABCD有30条记录,ASDF有10条记录。我想每小时拿到他们的记录。

示例输出将是:

station    call_time(H)     total_call
ABCD       10               5               // ABCD has 5 calls at 10am
ABCD       12               20              // ABCD has 20 calls at 12pm
ABCD       15               5               // ABCD has 5 calls at 3pm
ASDF       08               4               // ASDF has 4 calls at 8am
ASDF       05               2               // ASDF has 2 calls at 5pm
ASDF       13               2               // ASDF has 2 calls at 1pm
ASDF       22               2               // ASDF has 2 calls at 10pm

2 个答案:

答案 0 :(得分:5)

SELECT station, HOUR(call_time), COUNT(call_id) AS total_call
FROM call_log
GROUP BY station, HOUR(call_time)

答案 1 :(得分:1)

试试这个::

SELECT station, HOUR(call_time) as call_time(H), COUNT(call_id) AS total_call
FROM call_log
GROUP BY station, HOUR(call_time)