如何在MS-Access中将空值替换为零

时间:2011-06-13 12:53:32

标签: sql ms-access

我在Ms-Access中有以下查询,但我想用零替换Blank值,但我无法得到正确的答案。有没有办法在零中替换空白值。

(SELECT 
   SUM(IIF(Review.TotalPrincipalPayments,0,Review.TotalPrincipalPayments))+
   SUM(IIF(Review.TotalInterestPayments,0,Review.TotalInterestPayments ))
 FROM 
   tblReviewScalars as Review 
 INNER JOIN tblReportVectors AS Report ON(Review.LoanID=Report.LoanID) 
 WHERE Report.AP_Indicator="A" AND Report.CashFlowDate=#6/5/2011# AND Review.AsofDate=#6/5/2011# AND ( Review.CreditRating =ReviewMain.CreditRating)) AS [Cash Collected During the Period],

3 个答案:

答案 0 :(得分:1)

我认为TotalPrincipalPaymentsTotalInterestPayments都是数字类型,因此有问题的'空白'是NULL值。

在SQL中,set函数SUM将忽略NULL值,除非所有值都解析为NULL,在这种情况下返回NULL(错误地,错误是SQL不是Access for改变:)

要使用一个简单示例,SELECT SUM(a) FROM T;只有NULLa IS NULL的所有行或T为空时为{0}才会返回T。因此,您可以在NULL函数之外移动'替换SUM()为零'逻辑。注意到计算中“NULL s传播”,您需要为每个NULL处理SUM()

您尚未发布整个查询,例如相关名称的来源('table alias')ReviewMain不是showm。但是很明显,您正在构建一个名为"Cash Collected During the Period"的派生表,在这种情况下,您的计算列需要AS子句('列别名'),例如TotalPayments,例如... ( SELECT IIF(SUM(Review.TotalPrincipalPayments) IS NULL, 0, SUM(Review.TotalPrincipalPayments)) + IIF(SUM(Review.TotalInterestPayments) IS NULL, 0, SUM(Review.TotalInterestPayments)) AS TotalPayments FROM tblReviewScalars as Review INNER JOIN tblReportVectors AS Report ON Review.LoanID = Report.LoanID WHERE Report.AP_Indicator = 'A' AND Report.CashFlowDate = #2011-05-06# AND Review.AsofDate = #2011-05-06# AND Review.CreditRating = ReviewMain.CreditRating ) AS [Cash Collected During the Period], ...

{{1}}

答案 1 :(得分:0)

@ onedaywhen的答案的替代方法是使用nz函数,该函数专门用于空替换:

SELECT 
   SUM(NZ(Review.TotalPrincipalPayments,0))+
   SUM(NZ(Review.TotalInterestPayments,0))
...

正如oneday指出的那样,这在功能上等同于将函数放在聚合之外,这可能表现得更好(函数被调用一次,而不是每个未聚合行一次):

SELECT 
   NZ(SUM(Review.TotalPrincipalPayments),0)+
   NZ(SUM(Review.TotalInterestPayments),0)
...

答案 2 :(得分:0)

要在Access 2010数据库中将空值更改为零,请打开表格,转到设计视图,单击该字段并将默认值设置为:=0