我在MS Access数据库(Office 2013)中有一个表:
Table1
ID ACC_YEAR PROGRESS
1 2008-09 IP
2 2008-09 IP
3 2008-09 C
4 2009-10 IP
“ACC_YEAR”是一个文本字段,“PROGRESS”字段可能有四个可能的值:IP / C / NS / UCS。
现在我希望有一个明智的& PROGRESS明智计算报告如下:
ACC_YEAR IP C NS UCS PROGRESS_TOTAL
2008-09 2 1 0 0 3
2009-10 2 1 0 0 3
任何人都可以帮我为我构建SQL。
提前致谢。
答案 0 :(得分: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;