在访问2007中获取两列的计数

时间:2013-08-04 11:44:58

标签: sql ms-access select count ms-access-2007

TblAquestion:

AQcode(pk)|AQdescribe
---------------------------------
1          do you want to continue?     
2          what ..........?
3          this is a ..........
4          my name is a ali?


TblUserResult:

ID(pk)|AQcode_fk|Result
-----------------------
100    1         1
101    1         0
102    1         0
103    1         0
104    1         1

105    2         1
106    2         0

107    3         1
108    3         1
109    3         1
110    3         0

111    4         1


输出:

ResultYes is count of result where '1'
ResultNo is count of result where '0'
AQcode|AQdescribe                 |CountResultYes|CountResultNo
---------------------------------------------------------------
1      do you want to continue?    2              3
2      what ..........?            1              1
3      this is a ..........        3              1
4      my name is a ali?           1              0


SQL命令?

2 个答案:

答案 0 :(得分:2)

select q.AQcode, q.AQdescribe, 
       sum(IIF(r.Result = 1, 1, 0)) as CountResultYes,
       sum(IIF(r.Result = 0, 1, 0)) as CountResultNo
from TblAquestion q
left join TblUserResult r on r.AQcode_fk = q.AQcode
group by q.AQcode, q.AQdescribe

答案 1 :(得分:0)

select aqcode,aqdescribe,sum(Cno),sum(Cyes) from
(
select aqcode,aqdescribe,'0' as Cno,count(result) as Cyes
from TblAquestion aq,TblUserResult r
where aq.aqcode=r.aqcode and r.result='1'
group by aqcode,aqdescribe
Union
select aqcode,aqdescribe,count(result) as Cno,'0' as Cyes
from TblAquestion aq,TblUserResult r
where aq.aqcode=r.aqcode and r.result='0'
group by aqcode,aqdescribe
)
group by aqcode,aqdescribe