我在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],
答案 0 :(得分:1)
我认为TotalPrincipalPayments
和TotalInterestPayments
都是数字类型,因此有问题的'空白'是NULL
值。
在SQL中,set函数SUM
将忽略NULL值,除非所有值都解析为NULL
,在这种情况下返回NULL
(错误地,错误是SQL不是Access for改变:)
要使用一个简单示例,SELECT SUM(a) FROM T;
只有NULL
为a 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
。