First, you can get the counts using aggregation:
select answer, count(id)
from survey_ans
where question = 'q13'
group by answer;
I am guessing that the count(distinct)
really isn't necessary. count()
is better to use when you can.
To get the total, join that in:
select sa.answer, count(sa.id), tot.total
from survey_ans sa cross join
(select count(*) as total from survey_ans where question = 'q13') as tot
where question = 'q13'
group by sa.answer;
You can get the percentage with something like:
select sa.answer, 100.0*count(sa.id)/tot.total as percentage
from survey_ans sa cross join
(select count(*) as total from survey_ans where question = 'q13') as tot
where question = 'q13'
group by sa.answer;