计数呼叫在0到10秒的某些范围内应答,按计数分组

时间:2017-08-25 04:12:20

标签: mysql count group-by range intervals

我需要在0到10秒,0到20秒等特定范围内对呼叫进行计数。计数将增加,而增量将是当前和下一个之间的不同计数。 %将是当前计数/最终计数总数。

以下是可以与数据一起使用的sqlfiddle:http://sqlfiddle.com/#!9/803d2/2

callsdetails示例表:

+-----+----------------+----------+----------+---------------+
| id  | callid         | callerno | duration | status        |
+-----+----------------+----------+----------+---------------+
| 634 | 1479097551.228 | 1000     |        2 | complete      |
| 635 | 1479102518.248 | 1000     |       12 | complete      |
+-----+----------------+----------+----------+---------------+

预期结果:

+------------------------+----------+----------+----------+
| Ranges                 | Count    | Delta    |    %     | 
+------------------------+----------+----------+----------+
| Between 0 to 10 secs   |       44 | +44      |  84.62 % |
| Between 0 to 20 secs   |       48 | +4       |  92.31 % |
| Between 0 to 30 secs   |       50 | +2       |  96.15 % |
| Between 0 to 40 secs   |       51 | +1       |  98.08 % |
| Between 0 to 50 secs   |       51 | +0       |  98.08 % |
| Between 0 to 60 secs   |       51 | +0       |  98.08 % |
| Between 0 to 70 secs   |       51 | +0       |  98.08 % |
| Between 0 to 80 secs   |       52 | +1       | 100.00 % |
| Between 0 to 90 secs   |       52 | +0       | 100.00 % |
| Between 0 to 100+ secs |       52 | +0       | 100.00 % |
+------------------------+----------+----------+----------+
Total                            52

我现在能够创建的是查询以下,如果您可以帮助提供更好的解决方案,请咨询。我现在面临的问题是(优先级)我无法得到计数和(次要)%的最终计数总数(52),现在我手动输入最终计数总数(52)。请帮忙。

SELECT Ranges,Delta,ROUND(Delta/52*100,2) AS '%'
FROM
(
    SELECT 
    (
        IF(duration<=10,'10',IF(duration<=20,'20',IF(duration<=30,'30',
        IF(duration<=40,'40',IF(duration<=50,'50',
        IF(duration<=60,'60',IF(duration<=70,'70',IF(duration<=80,
        '80',IF(duration<=90,'90','100+')))))))))) 
        AS Ranges,COUNT(duration) AS Delta
        FROM callsdetails
        GROUP BY Ranges
    ) a
GROUP BY Ranges;

当前结果:

+--------+-------+-------+
| Ranges | Delta | %     |
+--------+-------+-------+
| 10     |    44 | 84.62 |
| 20     |     4 |  7.69 |
| 30     |     2 |  3.85 |
| 40     |     1 |  1.92 |
| 80     |     1 |  1.92 |
+--------+-------+-------+

1 个答案:

答案 0 :(得分:0)

如果我理解你的问题,那么你只需要在mysql中 ROLLUP 分组功能

您的查询

SELECT Ranges,Delta,ROUND(Delta/52*100,2) AS '%'
FROM
(
SELECT 
(
IF(duration<=10,'10',IF(duration<=20,'20',IF(duration<=30,'30',
IF(duration<=40,'40',IF(duration<=50,'50',
IF(duration<=60,'60',IF(duration<=70,'70',IF(duration<=80,
'80',IF(duration<=90,'90','100+')))))))))) 
AS Ranges,COUNT(duration) AS Delta
FROM callsdetails
GROUP BY Ranges
) a
GROUP BY Ranges WITH ROLLUP;

FOR MORE REFERENCE