-2

Tbl问题:

AQcode(pk)|AQdescribe
---------------------------------
1 你要继续吗?     
2 什么…………?
3 这是…………
4 我叫阿里?


用户结果:

ID(pk)|AQcode_fk|结果
----------------------
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 是“1”的结果计数
ResultNo 是“0”的结果计数
AQcode|AQdescribe |CountResultYes|CountResultNo
-------------------------------------------------- -------------
1 你要继续吗?2 3
2 什么…………?1 1
3 这是一个............ 3 1
4 我叫阿里?1 0


SQL 命令?

4

2 回答 2

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
于 2013-08-04T11:48:19.757 回答
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
于 2013-08-04T12:35:58.433 回答