多列上的SQL SUM和COUNT

时间:2017-04-10 19:59:04

标签: sql ms-access

我在MS Access中有数据,如:

+------------+-----------+
| 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

我已经到了

SELECT Format(DateValue(DateStart),"dddd") AS DayOFWeek,  PATIssuedYN,
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), PATIssuedYN;

问题是这会返回

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

1 个答案:

答案 0 :(得分: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);