We have a Result table with fields called QuestionID and Answer.
How can I get a query result where the result is group by the answer and its
percentage ?
Thanks.
For ex: Results table:
QuestionID Answer
1 aaa
1 aaa
1 ddd
2 yyyy
I want the result of the query for QuestionID = 1 to look like:
aaa 2 66.6666
ddd 1 33.3333
CREATE PROCEDURE GetResults
@.QuestionID numeric(9)
AS
SELECT Answer, COUNT(Answer) AS votes
FROM Results
WHERE (Question = @.QuestionID)
GROUP BY Answer
GOHello, fniles
Use something like this:
SELECT Answer,
COUNT(*) AS votes,
100. * COUNT(*) / (
SELECT COUNT(*)
FROM Results
WHERE QuestionID = @.QuestionID
) as Percentage
FROM Results
WHERE QuestionID = @.QuestionID
GROUP BY Answer
Razvan|||Thanks ! That works.
"Razvan Socol" <rsocol@.gmail.com> wrote in message
news:1133464892.892475.83390@.z14g2000cwz.googlegroups.com...
> Hello, fniles
> Use something like this:
> SELECT Answer,
> COUNT(*) AS votes,
> 100. * COUNT(*) / (
> SELECT COUNT(*)
> FROM Results
> WHERE QuestionID = @.QuestionID
> ) as Percentage
> FROM Results
> WHERE QuestionID = @.QuestionID
> GROUP BY Answer
> Razvan
>
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment