简体   繁体   中英

MS Access SQL - Count # of Records = TRUE

I have what seems fairly simple but am missing something here. I am trying to display all distinct [Business Process] values, and count the number of records for each [Business Process] in which [Corrective Action Is Required] = TRUE. There could be potentially zero records for some [Business Process] values that have [Corrective Action Is Require] = TRUE, or there could be values > 0. This is the SQL that I am using:

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];

Currently the SQL is showing a value of 0 in all records for [Total Corrective Action Is Required]. Can someone pin point what I am not doing correctly?

In Access, True=-1 and False=0, so how about:

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

You do not need Order By when you have Group By.

You can sum a boolean but in access you will get negative results as True = -1 - so this should work

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]

Wouldn't it be easier to do this?, also remember to check your underlying data.

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];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM