显示所有用户中答案选项的百分比

时间:2012-06-26 19:52:56

标签: php mysql sql sql-server

我有一个这样的表,有三个字段:

User | Question# | Answer

答案选择只有1,2,3,4或5

我需要编写一个SELECT语句,以便将答案1和2的百分比以及答案3,4和5的百分比一起打印出来。

结果应该是这样的:

Question# | %One+Two | %Three+Four+Five
1               %30          %70
2               %23          %77
.                .            .
.                .            .
.                .            .

我这样做是为了显示答案1和2的数量,但它没有提供我需要的东西:

select QNum, Response, Count(Response)
 from SurveyResults
 group by QNum, Response
 having Response<3;

1 个答案:

答案 0 :(得分:2)

select  QuestionNr
,       100.0 * count(case when Answer in (1,2) then 1 end) / count(*) as OneTwo
,       100.0 * count(case when Answer in (3,4,5) then 1 end) / count(*) as ThreeFourFive
from    YourTable
group by
        QuestionNr