2012-03-29 29 views
3

我有一個表稱爲[查看結果],看起來有點像下面這樣:計數SQL

[Reviewed By]....[Review Date]....[Corrective Action]....[CAR] 
John.............1/1/2011.........yes....................yes 
John.............2/5/2011.........No.....................yes 
John.............2/24/2011........yes....................yes 
Bobby............1/1/2011.........No.....................No 
Bobby............3/1/2011.........yes....................No 

我試圖通過評審在指定時間內顯示的[Corrective Action] = yes數量,也[CAR] = yes由評論者在指定時期內的數量。我嘗試使用以下的SQL,但它沒有給出正確的輸出:

select 
[Reviewed By], 
Count(IIF([Corrective Action] = yes, 1,0)) as [CAMBRs], 
Count(IIF([CAR] = yes,1,0)) as [CARs] 

from [Review Results] 

where [Review Date] between #1/1/2011# and #3/1/2011# 

group by 
[Reviewed By] 

有人可以用SQL指向正確的方向嗎?

+1

你指望所有的「不」的同時在查詢..相反計數使用總和..這將工作.. – Teja 2012-03-29 14:29:58

+0

賓果...那就是訣竅!謝謝 – JT2013 2012-03-29 14:43:10

+0

這很酷.... – Teja 2012-03-29 14:43:34

回答

5
select 
[Reviewed By], 
SUM(IIF([Corrective Action] = "yes", 1,0)) as [CAMBRs], 
SUM(IIF([CAR] = "yes",1,0)) as [CARs] 

from [Review Results] 

where [Review Date] between #1/1/2012# and #3/1/2012# 

group by 
[Reviewed By] 
+0

我應該提及** [糾正行動] **和** [CAR] **字段是表中的複選框** [回顧結果] ** – JT2013 2012-03-29 14:29:26

+0

在這種情況下,您可以使用Abs([糾正行動])返回1或0.'Count(Abs([Corrective Action])' – Fionnuala 2012-03-29 14:36:25

+0

你們真棒,像魅力一樣工作! – JT2013 2012-03-29 14:36:52

1

也許是這樣的:

select 
    [Reviewed By], 
    SUM(IIF([Corrective Action] = True, 1,0)) as [CAMBRs], 
    SUM(IIF([CAR] = True,1,0)) as [CARs] 

from [Review Results] 

where [Review Date] between #1/1/2012# and #3/1/2012# 

group by 
[Reviewed By] 
+0

這不起作用....如上所述** [糾正措施] **和** [汽車] **字段是** [檢查結果] **表格中的複選框.... – JT2013 2012-03-29 14:34:21

+0

剛剛更改了它。看一看 – Arion 2012-03-29 14:35:20

+0

你們真棒,工作過像一個魅力! – JT2013 2012-03-29 14:37:02