2015-07-02 77 views
0

我試圖根據月份對列'權重'= 2的所有金額進行求和並將其格式化爲美元貨幣。以下是我迄今爲止:基於條件的SQL Dsum

SELECT 
Format([Final Action Date],"yyyy-mm") AS [Month], 
DSum("[Amount]","C02: Underwriting Audit Case Detail Report Record Selection"," [Weight] = '2'") AS FA_Critical, 
Sum(IIf([Weight]="2",1,0)) AS Critical_Count 

FROM [C02: Underwriting Audit Case Detail Report Record Selection] 
WHERE ((([C02: Underwriting Audit Case Detail Report Record Selection].[Case Type]) Not In ("**Target IUP"))) 
GROUP BY Format([Final Action Date],"yyyy-mm"); 

這裏是我到目前爲止的結果:

Month FA_Critical Critical_Count 
2015-01 2035480  2 
2015-02 2035480  2 
2015-03 2035480  0 
2015-04 2035480  1 

下面是我想:

Month FA_Critical Critical_Count 
2015-01 $1,350,000 2 
2015-02 $510,480 2 
2015-03 $0   0 
2015-04 $175,000 1 

請幫助。 謝謝。

+0

這裏是我的表有更好的表現: – Robert

+0

這是訪問權? – mxix

+1

@mxix我希望你是對的(在SQL Server中沒有'DSUM'),所以要代理 – Richard

回答

1

您的問題在於DSum的標準,它在整個記錄集中查看[Weight] ='2',這就是爲什麼您獲得所有4條記錄相同的值。 DSUM不以與SUM相同的方式被GROUP BY分區。

使用SUM,而不是DSUM,而不是使用1,0真假,使用量列的值:

Sum(IIf([Weight]="2",[Amount],0)) AS FA_Critical 
+0

感謝您的幫助 - 非常感謝。我在sql中不太好 - 請告訴我爲什麼Access會出現一個錯誤消息:「子查詢不能用在表達式中(IIf [Weight] =」2「,[Amount],0))什麼是我做錯了嗎? – Robert

+0

既是[Weight]還是[Amount]列?或者是數量派生?我只能在你準備好的代碼片段中進行操作,而且你沒有任何原始數據。 –

+0

你的答案完美無缺! – Robert