2012-04-10 73 views
1

我有什麼看起來相當簡單,但我在這裏失去了一些東西。我試圖顯示所有不同的[業務流程]值,並計算[需要糾正措施] = TRUE的每個[業務流程]的記錄數。可能有一些[業務流程]潛在的零記錄有[解決方法是要求]值= TRUE,或者有可能是值> 0。這是我使用的SQL:MS Access SQL - 計數記錄數= TRUE

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值。有人能指出我做得不對嗎?

+0

真棒,是有道理的 – JT2013 2012-04-10 15:24:38

回答

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

上述說法的問題在於,它不顯示所有唯一的[業務流程]值。您的陳述僅顯示滿[需要糾正措施] = TRUE的[Business Process]值。 – JT2013 2012-04-10 14:28:33

3

在Access中,真= -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] 

你不需要排序,當你有分組依據。

1

可以總結一個布爾值,但在訪問您將得到陰性結果爲真= -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] 
+1

這是Access,你可以求和一個布爾值,yes = -1,no = 0。看到我對這個問題的回答。 – Fionnuala 2012-04-10 14:52:12

+0

@Remou和DJ。你們兩個人的方法都有效 – JT2013 2012-04-10 14:54:16