2017-04-10 152 views
0

我有這樣的MS Access數據:SQL SUM和COUNT多個列

+------------+-----------+ 
| DateStart | PATIssued | 
+------------+-----------+ 
| 12/12/2004 | Yes  | 
+------------+-----------+ 
| 13/12/2004 | No  | 
+------------+-----------+ 
| 14/12/2004 | No  | 
+------------+-----------+ 
| 12/12/2004 | No  | 
+------------+-----------+ 
| 12/12/2004 | Yes  | 
+------------+-----------+ 
| 14/12/2004 | No  | 
+------------+-----------+ 
| 13/12/2004 | Yes  | 

我想星期幾來算,每天記錄,在過去的一週號碼,並在另一列數記錄,其中PATIssued =是

那麼結果會是什麼樣子:

Date  DayCount PATCount 
Monday  3  2 
Tuesday  2  1 
Wednesday  2  0 

我已經儘可能

得到

的問題是,這將返回

Date  PATIssuedYN DayCount PATCount 
Monday  No   1   
Monday  Yes   2  2 
Tuesday  No   1 
Tuesday  Yes   1  1 
Wednesday  No   2   
Wednesday  Yes   0  0 

,其中星期一爲12/12,週二週三13/12 14/12是

+0

刪除'從你的選擇和組PATIssuedYN'由 – JamieD77

+0

使用條件,即PATIssuedYN =是 –

+1

@Евг нийКондратенко會扭曲'DayCount'值 – JamieD77

回答

1

,因爲你不希望返回列PatIssuedYN,將其刪除從你的選擇和你的小組是:

SELECT Format(DateValue(DateStart),"dddd") AS DayOFWeek, 
Count(Table1.DateStart) AS DayCount, 
Sum(IIf([PATIssuedYN]="Yes",1,)) 
FROM Table1 
WHERE (((Table1.DateStart) Between DateAdd("d",-7,Now()) And Now())) 
GROUP BY DateValue(DateStart);