mysql select统计语句简单解析
1. mysql 的select 可以通过聚集函数等用来做统计,而且功能强大
2. 下面我通过一个例子来说明 统计语句的强大,此例子 是用来统计一个队列报表,数据来自一个 queue 表,该表导出来的sql语句如下:
DROP TABLE IF EXISTS `queue`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `queue` (
`id` int(11) NOT NULL auto_increment,
`queue` varchar(10) NOT NULL COMMENT '队列号码',
`calltime` datetime default NULL COMMENT '呼入队列时间,可以计算等待时间',
`src` varchar(20) default NULL COMMENT '主叫id',
`status` enum('ANSWERED','NO ANSWER') NOT NULL default 'NO ANSWER' COMMENT '呼叫结果',
`ringtimes` int(11) default '0' COMMENT '队列中坐席振铃次数或者是漏接次数',
`answertime` datetime default NULL COMMENT '坐席应答时间,可以计算等待时间',
`hanguptime` datetime default NULL COMMENT '挂机时间,用来计算平均通时长',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=21 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `queue`
--
LOCK TABLES `queue` WRITE;
/*!40000 ALTER TABLE `queue` DISABLE KEYS */;
INSERT INTO `queue` VALUES (1,'901','2013-08-09 15:57:30','801','ANSWERED',2,'2013-08-09 15:57:36','2013-08-09 15:57:44'),(2,'902','2013-08-09 16:17:21','801','ANSWERED',4,'2013-08-09 16:18:07','2013-08-09 16:18:14'),(17,'902','2013-08-09 17:24:23','801','ANSWERED',1,'2013-08-09 17:24:27','2013-08-09 17:24:30'),(16,'902','2013-08-09 17:23:48','801','ANSWERED',1,'2013-08-09 17:23:51','2013-08-09 17:23:53'),(15,'902','2013-08-09 17:23:34','801','NO ANSWER',1,'2013-08-09 17:23:36','2013-08-09 17:23:36'),(11,'902','2013-08-09 17:13:10','801','NO ANSWER',1,'2013-08-09 17:13:12','2013-08-09 17:13:12'),(12,'902','2013-08-09 17:13:57','801','NO ANSWER',1,'2013-08-09 17:14:00','2013-08-09 17:14:00'),(14,'902','2013-08-09 17:23:22','801','NO ANSWER',1,'2013-08-09 17:23:25','2013-08-09 17:23:25'),(18,'902','2013-08-09 17:28:41','801','ANSWERED',1,'2013-08-09 17:28:44','2013-08-09 17:28:46'),(19,'902','2013-08-09 17:31:43','801','ANSWERED',0,'2013-08-09 17:31:47','2013-08-09 17:31:49'),(20,'902','2013-08-09 17:32:09','801','NO ANSWER',3,'2013-08-09 17:32:48','2013-08-09 17:32:48');
表内容截图如下:
3. 下面开始测试一些 select的统计命令
1)统计总条数
SELECT COUNT(*) FROM queue
2)统计接听了电话的队列数
SELECT COUNT(*)
FROM queue
WHERE status= 'ANSWERED' ;
其实还可以这样,而且以后我们就会发现这样的好处了,如下:
SELECT SUM(`status` = 'ANSWERED')
FROM queue
说明:mysql中,逻辑表达式的值要某是0,要某是1 ,例如: select "aaa"="bbb"; 将会返回0 。所以上面的 SELECT SUM(`status` = 'ANSWERED') 返回的将是符合条件的 记录的个数的累加。
3)要求从上面的表中一次性的统计出:
日期 队列名 呼入次数 接通次数 失败次数 漏接次数 (失败率 漏接率) 平均等待时长 平均通话时长
我可以先用如下语句一次性的统计出:
日期 队列名 呼入次数 接通次数 失败次数 漏接次数 等待总时长 通话总时长
SELECT DATE(calltime) AS `date`,queue,COUNT(*) AS totalcalls,SUM(STATUS='ANSWERED') AS answeredtimes,SUM(STATUS='NO ANSWER') AS noansweredtimes,SUM(ringtimes) AS passedcalltimes,SUM(TIMESTAMPDIFF(SECOND,calltime,answertime)) AS waittotalsecond,SUM(TIMESTAMPDIFF(SECOND,answertime,hanguptime)) AS during
FROM queue
GROUP BY DATE(calltime),queue
ORDER BY DATE(calltime) ASC,queue ASC
然后,再在php服务器端 计算出 “(失败率 漏接率) 平均等待时长 平均通话时长” ,在显示到客户端 浏览器页面上。
说明:上面的SQL语句,采用了日期和queue 两个来分组,另外,也支持 SUM(TIMESTAMPDIFF(SECOND,calltime,answertime)) 这样的求总和的方法。GROUP BY里面可以是函数。
其它例子:
SELECT DATE(calldate),workno,dst,COUNT(*),SUM(billsec),AVG(billsec)
FROM cdr
WHERE dst IN (SELECT extension FROM bgcrm.users WHERE LENGTH(extension)>2 AND LENGTH(extension)<6)
GROUP BY DATE(calldate),workno
HAVING workno IS NOT NULL
ORDER BY DATE(calldate) ASC,workno ASC
说明:HAVING 里面的语句,必须是GROUP BY中的字段,不同于WHERE 语句。