2014-08-28 41 views
0

我現在有這樣的查詢:MySQL - 是一個子查詢這裏需要什麼?

SELECT 
    sec_to_time(avg(t1.sessiontime)) as aloc, 
    count(*) as calls 
FROM 
    table1 AS t1 
    inner join 
    table2 as t2 ON t1.destination = t2.prefix 
WHERE 
    t1.card_id = '101' 
AND 
    t1.terminatecauseid = 1 
group by t1.destination 

實施例的結果:

enter image description here

的 '呼叫' 的數據被綁定到 't1.terminatecauseid = 1'(僅應答的呼叫的意思)

我希望在所有通話中有一定比例的回答。 沒有條件的相同查詢(t1.terminatecauseid = 1)會給我所做的總調用。

我想知道什麼是添加所謂的「成功率平均爲」另一列,會做的最好辦法: 總通話*成功的呼叫/ 100 是一個子查詢這裏什麼需要?或一個全新的和不同的查詢?

回答

1
SELECT 
    sec_to_time(avg(t1.sessiontime)) as aloc, 
    sum(t1.terminatecauseid = 1) * 100/count(*) as Average_Success_Rate, 
    sum(t1.terminatecauseid = 1) as calls 
FROM 
    table1 AS t1 
    inner join 
    table2 as t2 ON t1.destination = t2.prefix 
WHERE 
    t1.card_id = '101' 
group by t1.destination 
+0

更改爲'sum(t1.terminatecauseid = 1)as calls'而不是'count(*)as calls'返回不同的結果,您的方式會更好嗎? – 2014-08-28 10:21:23

+0

我看不出爲什麼它應該返回不同的結果。 'count(*)'也計數'null'記錄,但這不應該是這種情況。你可以在[SQLFiddle](http://sqlfiddle.com/)中提供一些示例數據嗎? – 2014-08-28 10:23:32

+0

它的確如此。我試圖瞭解我的方式是否是一個錯誤。結果是不同的 – 2014-08-28 10:25:26