2017-08-25 66 views
1

我需要在特定範圍內0到10秒,0到20秒等內應答呼叫計數。計數將增加,而增量將是不同的計數當前和下一個之間。該百分比將是當前計數/最終計數總數。計數呼叫在一定範圍內應答0到10秒,按組計數

這裏是你可以用數據用於測試的sqlfiddle:callsdetails的http://sqlfiddle.com/#!9/803d2/2

樣品表:

+-----+----------------+----------+----------+---------------+ 
| 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 | 
+--------+-------+-------+ 
+0

你可以將'UNION'總數計算到最低。原來的桌子怎麼樣? – RealCheeseLord

+0

我在上面的示例表中添加了對原始表格的引用。請指教。 – DarkSilver

回答

0

如果我理解你的問題,那麼你只是想ROLLUP分組funtion在MySQL

查詢

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

+0

嗨denny,謝謝你的信息。你可以看看上面的「當前結果」,我如何在「計數」中添加每個範圍內增加的「預期結果」,因爲這應該是所需的總量。 現在,當我運行查詢時,它在底部添加了1個新行。 | NULL | 1 | 1.92 | ROLLUP也只能用於顯示總數?我無法使用%的值? %公式是當前計數/最終計數總數。 再次感謝您的幫助,如果您知道,請諮詢。 – DarkSilver

+0

給我sqlfiddle – denny

+0

嗨denny,這裏是sqlfiddle。 http://sqlfiddle.com/#!9/803d2/1讓我知道你是否有更好的解決方案。謝謝你的幫助。 – DarkSilver

相關問題