我在Access 2013数据库中有一个查询,它会计算符合特定条件的记录数量。但是,有些情况下没有记录匹配,查询也不返回任何内容。这会导致问题,因为我在计算中使用此值。请从下面的查询中找到SQL代码。
SELECT [Main Database].Renewal,
[Main Database].[2014/15 - Permission to Review],
Count(Main Database].[2014/15 - Permission to Review]) AS [CountOf2014/15 - Permission to Review]
FROM [Main Database]
GROUP BY [Main Database].Renewal, [Main Database].[2014/15 - Permission to Review]
HAVING ((([Main Database].Renewal) Like "**/05/****") AND (([Main Database].[2014/15 - Permission to Review])="Yes"));
我尝试过使用Nz函数和IIF(IsNull([])),但这些都不起作用。
我做错了什么或找到我想要的替代方法吗?
编辑 - 带子查询的新代码
SELECT COUNT (*) AS iniSumNov
FROM (
SELECT [Main Database].Renewal, [Main Database].[2014/15 - Initial Contact],
Count([Main Database].[2014/15 - Initial Contact])
AS [CountOf2014/15 - Initial Contact]
FROM [Main Database]
GROUP BY [Main Database].Renewal, [Main Database].[2014/15 - Initial Contact]
HAVING ((([Main Database].Renewal) Like "**/11/****") AND (([Main Database].[2014/15 - Initial Contact])="Yes"))
);
答案 0 :(得分:0)
使用此.. ..
SELECT A.Renewal as Renewal ,
A.[2014/15 - Permission to Review] as Permission_to_Review,
IIF(isNull(Count(A.[2014/15 - Permission to Review])),0,Count(A.[2014/15 - Permission to Review])) AS count_Permission_to_Review
FROM [Main Database] A
where (((A.Renewal) Like "**/05/****") AND ((A.[2014/15 - Permission to Review])="Yes"))
GROUP BY A.Renewal, A.[2014/15 - Permission to Review]
;
答案 1 :(得分:0)
在子查询上使用COUNT(*):
SELECT COUNT(*) as NbRecords
FROM (
SELECT ...whatever you need
)
如果子查询为空,则主查询将返回NbRecords = 0