2013-08-22 26 views
0

我無法使計算列顯示平均值。請參閱:在合併值上使用滾動平均值

Cast(AVG(COALESCE(mnth1.HandledCalls,0)+COALESCE(mnth2.HandledCalls,0)+COALESCE(mnth3.HandledCalls,0)) as Decimal(8,2)) as Avg_Handled, 

如何讓此顯示平均顯示3列?有時,mnth2和mnth3可能不會填充。如果在任何給定的時間任何值都爲空,我試圖用合計的月份進行計數。我收到一個錯誤,說選擇列表無效。有什麼建議麼?

+0

你可以添加表結構到你的問題? –

+0

當你計算一個列的值爲NULL的AVG時,是否要將NULL視爲ZERO,還是隻需要其他兩列的平均值? –

+0

表結構僅選擇mnth1,mnth2,mnth 3和每個可用值的平均值。我正在使用teradata 12.而且,當我想把它當作不存在的時候(又名其他2列的平均值) – ciw916

回答

1

這樣的事情?

cast(
    sum(
     coalesce(mnth1.HandledCalls, 0) + 
     coalesce(mnth2.HandledCalls, 0) + 
     coalesce(mnth3.HandledCalls, 0) 
    ) 
as decimal(29, 10))/
(
    count(mnth1.HandledCalls) + 
    count(mnth2.HandledCalls) + 
    count(mnth3.HandledCalls) 
) 
+0

看起來很簡單...我會測試我什麼時候回來在我的桌子上。謝謝你的回覆。 – ciw916

+0

其實我不知道它是否會在Teradata工作,但你可以試試 –

0

,你可以這樣做

Cast(
    AVG((SELECT COALESCE(mnth1.HandledCalls,0) 
      UNION ALL SELECT COALESCE(mnth2.HandledCalls,0) 
      UNION ALL SELECT COALESCE(mnth3.HandledCalls,0))) 
    AS Decimal(8,2)) as Avg_Handled 
0

你有沒有試過如下:

SELECT CAST(AVG(ZEROIFNULL(mnth1.HandledCalls) 
    + ZEROIFNULL(mnth2.HandledCalls) 
    + ZEROIFNULL(mnth3.HandledCalls) as Decimal(8,2)) as Avg_Handled 
FROM .... 

默認爲空值由Teradata的聚合函數忽略。