简体   繁体   English

查询中的MySQL查询

[英]MySQL query within Query

I have the following query which works well to query asterisk cdr for all missed queue calls but it is duplicating. 我有以下查询,可以很好地查询所有错过的队列调用的星号cdr,但它是重复的。 That is, if there are two extensions in the queue call, then it's classified as two missed calls. 也就是说,如果队列呼叫中有两个分机,则将其分类为两个未接呼叫。

The query: 查询:

SELECT count( * ) as today_total_8
    FROM cdr 
    WHERE channel LIKE '%from-queue%' 
        AND disposition='NO ANSWER' 
        AND DATE(`calldate`) = DATE(CURDATE())

The column calldate is the same value for all two results; 这两个结果的列calldate值相同。 how can I add this to the above query to display count to one missed call? 如何将其添加到上面的查询中以显示未接来电的计数?

Data returned on SELECT without count: SELECT上返回的数据不计:

calldate            channel                 disposition     
------------------------------------------------------------------------
2014-02-08 01:15:02     Local/201@from-queue-00000012;2     NO ANSWER
2014-02-08 01:15:02     Local/200@from-queue-00000013;2     NO ANSWER
2014-02-08 00:18:04     Local/201@from-queue-00000010;2     NO ANSWER
2014-02-08 00:18:04     Local/200@from-queue-00000011;2     NO ANSWER

How can I do this, as I want my count to be 2? 我希望我的人数为2,该怎么做?

SELECT count(distinct calldate ) as today_total_8
FROM cdr 
WHERE channel LIKE '%from-queue%' 
    AND disposition='NO ANSWER' 
    AND DATE(`calldate`) = DATE(CURDATE())
GROUP BY calldate

Change the SELECT list of the query to something like this: 将查询的SELECT列表更改为如下所示:

SELECT COUNT(DISTINCT SUBSTRING_INDEX(cdr.channel,'@',1) AS today_total_8
  FROM ...

That will prevent "duplicate" values of the extension (the portion of channel preceding the '@' sign) from being counted more than once. 这将防止重复数扩展名(“ @”符号前面的通道部分)的“重复”值。

This is using the SUBSTRING_INDEX function to return just the "extension", eg 'Local/201' . 这是使用SUBSTRING_INDEX函数仅返回“扩展名”,例如'Local/201'

The DISTINCT keyword inside the COUNT aggregate adds 1 to the count for for each unique value of "extension". COUNT集合内的DISTINCT关键字为“扩展”的每个唯一值的计数加1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM