SQL 用于获取调查所选答案百分比的服务器查询

SQL Server query for getting percentage of survey selected answers

我在 asp.net 做了一个调查。调查分为三种类型,包含不同的问题。用户可以从五个答案中选择 select:

strongly agree(5), 
agree(4), 
neutral(3), 
disagree(2), 
strongly disagree(1)

我得到以下形式的结果。

我想以下面的形式显示结果。

我的问题是 SQL 查询如何获得从强烈不同意到强烈同意的每个 selected 答案的百分比,如上图所示。

这样就可以了:

select sq.QuestionId,sq.QuestionText, count(ca.AnswerOptionScore) as TotalAnswered,

sum(case when ca.AnswerOptionScore=5 then 1 else 0 end) * 100/CONVERT(decimal(4,2), count(ca.AnswerOptionScore)) as 'StronglyAgreePercent',

sum(case when ca.AnswerOptionScore=4 then 1 else 0 end) * 100/CONVERT(decimal(4,2), count(ca.AnswerOptionScore)) as 'AgreePercent',

sum(case when ca.AnswerOptionScore=3 then 1 else 0 end) * 100/CONVERT(decimal(4,2), count(ca.AnswerOptionScore)) as 'NeutralPercent',

sum(case when ca.AnswerOptionScore=2 then 1 else 0 end) * 100/CONVERT(decimal(4,2), count(ca.AnswerOptionScore)) as 'DisagreePercent',

sum(case when ca.AnswerOptionScore=1 then 1 else 0 end) * 100/CONVERT(decimal(4,2), count(ca.AnswerOptionScore)) as 'StronglyDisagreePercent'

from Suvey_Completed_Answers ca

inner join Survey_Questions sq on sq.QuestionId = ca.QuestionID
 Where ca.SurveyId = 3

group by sq.QuestionText,sq.QuestionText,sq.QuestionId