我有3个选择查询,返回2列设置。
有quizno
,correct
,wrong
和notattempted
列可以像以下一样出现:
quizno correct wrong notattempted
1 80 10 10
2 60 20 20
3 100 0 0
这些是分开的查询:
select quizno, count(*) as correct from v_t1 where examid=96 AND result='correct'
group by quizno order by count(*) desc
select quizno, count(*) as wrong from v_t1 where examid=96 AND result='wrong'
group by quizno order by count(*) desc
select quizno, count(*) as notattempted from v_t1 where examid=96 AND result='notattempted'
group by quizno order by count(*) desc
答案 0 :(得分:3)
您可以使用CASE聚合并获得预期的输出
select quizno,
sum( case when result='correct' then 1 else 0 end) as 'correct',
sum( case when result='wrong' then 1 else 0 end) as 'wrong',
sum( case when result='notattempted' then 1 else 0 end) as 'notattempted'
from v_t1
where examid = 96
group by quizno