MS Access SQL - 计数记录数= TRUE

时间:2012-04-10 14:18:44

标签: sql ms-access

我有一些看似相当简单的东西,但我在这里遗漏了一些东西。我试图显示所有不同的[业务流程]值,并计算[需要纠正措施] = TRUE的每个[业务流程]的记录数。对于[纠正措施需要] = TRUE,或者可能存在值>的某些[业务流程]值,可能存在零记录。这是我正在使用的SQL:

SELECT  [Business Process], sum([Corrective Action Is Required]) as [Total Corrective Action Is Required]

FROM [Review Results]

WHERE [Corrective Action is Required] in 

(select IIF([Corrective Action Is Required] = TRUE, 1, 0) from [Review Results]) 

GROUP BY [Business Process]

ORDER BY [Business Process];

目前,SQL在[所需的总纠正措施]的所有记录中显示的值为0。有人可以指出我做的不正确吗?

3 个答案:

答案 0 :(得分:3)

在Access中,True = -1且False = 0,那么如何:

SELECT [Business Process], 
       Sum(Abs([Corrective Action Is Required])) 
          as [Total Corrective Action Is Required]
FROM [Review Results]
GROUP BY [Business Process]

当你有Group By时,你不需要Order By。

答案 1 :(得分:1)

你可以加一个布尔值,但在访问中你会得到负结果,因为True = -1 - 所以这应该有效

SELECT  [Business Process], sum(iif([Corrective Action Is Required] = TRUE, 1, 0)) as [Total Corrective Action Is Required]
FROM [Review Results]
GROUP BY [Business Process]
ORDER BY [Business Process]

答案 2 :(得分:0)

这样做会不会更容易?还要记得查看基础数据。

SELECT  [Business Process], count([Corrective Action Is Required]) as [Total Corrective Action Is Required]
FROM [Review Results]
WHERE [Corrective Action Is Required] = 1
GROUP BY [Business Process]
ORDER BY [Business Process];