问卷调查统计查询

1.需求了解,导出一份调查问卷每个选项的所占的百分比与票数

2.表的介绍:问卷表 Questionnaire

                     题目表 JY_Question 

                     选项表 JY_Option 

                     问卷记录表 JY_PollResult

select e.Title as '题目' ,d.Title as '选项' ,c.OptionCount as '票数',c.OptionPer as '百分比' from (
       select a.OptionId,COUNT(a.OptionId) as OptionCount, convert(decimal(10,2),COUNT(a.OptionId))/
                 (select COUNT(*) from JY_PollResult
                 where QuestionId in(
                 select top 1 id from JY_Question
                 where Target=9
))*100 as OptionPer from JY_PollResult a ,JY_Question b
where a.QuestionId=b.id
and b.Target=9
group by a.OptionId

    ) as c
join JY_Option d on c.OptionId=d.id
join JY_Question e on d.questionId=e.id
order by e.id

原文地址:https://www.cnblogs.com/smalldragon-hyl/p/8005507.html