2015-09-17 25 views
1

我在MS Access数据库中的表(办公室2013):MS访问:在一个连续多个特定值的计数

Table1 
ID ACC_YEAR PROGRESS 
1 2008-09  IP 
2 2008-09  IP 
3 2008-09  C 
4 2009-10  IP 

“ACC_YEAR”是一个文本字段和“进步”场可能有四个可能的值:IP/C/NS/UCS。

现在我想有一年明智&进度明智盘点报告是这样的:

ACC_YEAR IP  C  NS  UCS PROGRESS_TOTAL 
2008-09  2  1  0  0  3 
2009-10  2  1  0  0  3 

请人帮助构建SQL我。

在此先感谢。

+0

GROUP BY,计数(...的情况下) – jarlh

回答

2

一种方法是有条件聚集:

select acc_year, 
     sum(iif(Progress = "IP", 1, 0)) as IP, 
     sum(iif(Progress = "C", 1, 0)) as C, 
     sum(iif(Progress = "NS", 1, 0)) as NS, 
     sum(iif(Progress = "UCS", 1, 0)) as UCS, 
     count(*) 
from table 
group by acc_year 
order by acc_year; 
+0

非常感谢您的快速回复,这真是棒极了... – Arunabha