2

我正在使用这个表达式:

SELECT DATE(calldate) as Data,COUNT(clid) as Registros,dst as Tronco
FROM cdr WHERE dst=55008133070000 
AND calldate BETWEEN '2012-10-01' AND '2012-10-15'
GROUP BY DATE(calldate), clid HAVING COUNT(clid) > 1 ORDER BY Data DESC

Date---------Total------ Trunk
2012-10-11  3   55008133070000
2012-10-11  2   55008133070000
2012-10-11  3   55008133070000
2012-10-10  2   55008133070000
2012-10-10  2   55008133070000
2012-10-10  2   55008133070000
2012-10-09  3   55008133070000
2012-10-09  2   55008133070000
2012-10-09  2   55008133070000
2012-10-09  3   55008133070000
2012-10-09  5   55008133070000
2012-10-09  5   55008133070000

但是,需要显示当天重复的次数。

Date---------Total------ Trunk
2012-10-11  3   55008133070000
2012-10-10  3   55008133070000
2012-10-09  6   55008133070000

有什么建议么?

4

3 回答 3

2

在您的查询中,您正在使用GROUP BY DATE(calldate), clidwhich 将在每一天为您COUNT()提供每个clid

clid从子句中删除GROUP BY以获取每个日期的计数。

您的最终查询类似于:

SELECT
    DATE(calldate) as Data, COUNT(*) as Registros, dst as Tronco
FROM
    cdr
WHERE
    dst=55008133070000 
    AND calldate BETWEEN '2012-10-01' AND '2012-10-15'
GROUP BY
    DATE(calldate)
HAVING
    COUNT(clid) > 1
ORDER BY
    Data DESC

但是,在此查询中,拉动dst可能没有意义,可以被删除。

于 2012-10-17T14:06:47.557 回答
0

编辑由于OP评论:

您应该按dst而不是按分组clid,还要计算不同的目的地编号,请参阅 GROUP BY 子句和 COUNT:

SELECT DATE(calldate) as Data,COUNT( distinct clid) as Registros, dst as Tronco
FROM cdr 
WHERE dst=55008133070000 
      AND calldate BETWEEN '2012-10-01' AND '2012-10-15'
GROUP BY DATE(calldate),  dst 
HAVING COUNT(distinct clid) > 1 ORDER BY Data DESC
于 2012-10-17T14:07:03.557 回答
0

使用 Calldate 代替 Id。

SELECT DATE(calldate) as Data,COUNT(calldate) as Registros,dst as Tronco
FROM cdr WHERE dst=55008133070000 
AND calldate BETWEEN '2012-10-01' AND '2012-10-15'
GROUP BY DATE(calldate), dst HAVING COUNT(calldate) > 1 ORDER BY Data DESC
于 2012-10-17T14:08:58.533 回答