2016-06-01 13 views
0

我有一個查詢中使用如下圖所示計數不同組通過在select語句

select sum(amount)/count(id) from tabel1 where name ='sam'; 

我有這樣

name id transaction_type_id transaction_type 
sam 1 23     direct 
sam 1 56     direct 
sam 1  21    indirect 
sam 1  34    indirect 

表時,我也算(ID),我得到的答案'4',但我希望它是'2',因爲它有2個transaction_type'直接和間接'。請幫助我。

感謝

+1

'計數(不同TRANSACTION_TYPE)'? – jarlh

+2

'金額'欄是什麼?它從哪裏來的? – sagi

+0

請顯示您的預期結果。我不明白「我希望它是'2',因爲它有2個transaction_type」。你想計算不同的交易類型嗎?或者你在談論每種交易類型的平均ID數量?還有什麼?如果有三個「直接」而不是兩個,它會是另一個「數量」嗎? –

回答

0

試試這個:

select sum(amount)/count(distinct transaction_type) 
from tabel1 
where name ='sam' 

這將返回的2計數,因爲只有2不同transaction_type值。

0

嘗試使用:

select sum(amount)/count(distinct transaction_type) from tabel1 where name ='sam'; 
0

試試這個:

SELECT 
    SUM(amount)/COUNT(id) 
FROM tabel1 
WHERE 
    name = 'sam' 
GROUP BY 
    transaction_type;