我创建了一个数据库,我们可以跟踪我们与开发人员提出的错误(表:ApplixCalls),并跟踪与记录的错误相关的任何信件(表:通信)。
我试图创建一个计数,我们可以看到没有通信或只有来自我们的通信的错误数量。这应该让我们可以看到我们应该在哪里追逐我们的开发人员进行更新等。
到目前为止,我有这个SQL:
SELECT DISTINCT Count(ApplixCalls.OurRef) AS CountOfOurRef
FROM ApplixCalls LEFT JOIN Correspondence ON ApplixCalls.OurRef = Correspondence.OurRef
HAVING (((Correspondence.OurRef) Is Null)
AND ((ApplixCalls.Position)<>'Closed'))
OR ((ApplixCalls.Position)<>'Closed')
AND ((Correspondence.[SBSUpdate?])=True);
我发现这个部分正在计算我们发送更新的每个场合,当我需要它计算1时,OurRef是唯一的,它只有我们的更新:
OR ((ApplixCalls.Position)<>'Closed')
AND ((Correspondence.[SBSUpdate?])=True);
希望这有意义......
有解决方法吗?
答案 0 :(得分:2)
MS Access不支持count(distinct)
。在您的情况下,您可以使用子查询。此外,您的查询不应该工作。也许这就是你想要的:
SELECT COUNT(*)
FROM (SELECT ApplixCalls.OurRef
FROM ApplixCalls LEFT JOIN
Correspondence
ON ApplixCalls.OurRef = Correspondence.OurRef
WHERE (((orrespondence.OurRef Is Null) AND (ApplixCalls.Position) <> 'Closed')) OR
(ApplixCalls.Position <> 'Closed') AND (Correspondence.[SBSUpdate?] = True))
)
GROUP BY ApplixCalls.OurRef
) as x;
修改:
HAVING
子句没有GROUP BY
。我认为这应该是WHERE
(虽然我不是100%肯定你想要的逻辑)。SELECT DISTINCT
替换为SELECT . . . GROUP BY
。COUNT(DISTINCT)
现在是COUNT(*)
,带有子查询。编辑:
根据您评论中的说明:
SELECT COUNT(*)
FROM (SELECT ApplixCalls.OurRef
FROM ApplixCalls LEFT JOIN
Correspondence
ON ApplixCalls.OurRef = Correspondence.OurRef
WHERE (((orrespondence.OurRef Is Null) AND (ApplixCalls.Position) <> 'Closed')) OR
(ApplixCalls.Position <> 'Closed') AND (Correspondence.[SBSUpdate?] = True))
)
GROUP BY ApplixCalls.OurRef
HAVING SUM(IIF(Correspondence.[SBSUpdate?] = False, 1, 0)) = 0
) as x;
答案 1 :(得分:0)
如果您计算所有响应您条件的元素,那么您不需要DISTINCT ..如果删除重复结果则不同
SELECT Count(distinct ApplixCalls.OurRef) AS CountOfOurRef
FROM ApplixCalls LEFT JOIN Correspondence ON ApplixCalls.OurRef = Correspondence.OurRef
WHERE (((Correspondence.OurRef) Is Null)
AND ((ApplixCalls.Position)<>'Closed'))
OR ((ApplixCalls.Position)<>'Closed')
AND ((Correspondence.[SBSUpdate?])=True);
答案 2 :(得分:0)
我无法理解你为什么使用having子句。我希望这个查询能够满足您的需求。
SELECT DISTINCT Count(ApplixCalls.OurRef) AS CountOfOurRef
FROM ApplixCalls LEFT JOIN Correspondence ON ApplixCalls.OurRef = Correspondence.OurRef
HAVING (((Correspondence.OurRef) Is Null)
AND ((ApplixCalls.Position)<>'Closed'))
OR ((ApplixCalls.Position)<>'Closed')
AND ((Correspondence.[SBSUpdate?])=True);