tsql - How can I get percentage in sql? -
[getpollresult] @poll_form_id int begin select a.question_id,a.title,count(*) vote tbl_poll_option join tbl_poll_answer b on a.id=b.option_id join tbl_poll_question c on a.question_id=c.id poll_form_id=@poll_form_id group a.title,a.question_id end
i have poll system. use query count of every answer of question. how can percentage of every answered option this:
question 1 ---> option1=20.13 % ---> option2=79.87 %
question 2 ---> option3=100 %
question 3 ---> option4=30 % ---> option5=70 %
....
you can try following:
[getpollresult] @poll_form_id int begin select distinct a.question_id,a.title,count(*) over(partition a.question_id,a.title) / count(*) over(partition a.question) vote_percent tbl_poll_option join tbl_poll_answer b on a.id=b.option_id join tbl_poll_question c on a.question_id=c.id poll_form_id=@poll_form_id end
Comments
Post a Comment