View Full Version : % Query


latchmasterflex
01-21-2010, 05:14 AM
I have compiled a table of questions with the possible answer of 1 - 4.

I would like to be able to display which % of people selected each of the numbers.

At the minute I just have ="1" and so on

jzwp22
01-25-2010, 08:42 AM
First, you will need a query that counts the number of each response for each question

query name: qryCountRespPerQues
SELECT tblResponses.fkQuestionID, tblResponses.Response, Count(tblResponses.Response) AS CountOfResponse
FROM tblResponses
GROUP BY tblResponses.fkQuestionID, tblResponses.Response;

Then you will need a query to determine the total number of people who responded to each question

query name: qryCountAllByQues
Select questionID, count(questionID) as TotalResponding
From your table
Group by questionID




Now bring those 2 queries together in a third query and join via the questionID
SELECT qryCountAllByQues.fkQuestionID, qryCountRespPerQues.Response, qryCountRespPerQues.CountOfResponse/ qryCountAllByQues.CountOffkQuestionID*100 as PercentForThisResponse
FROM qryCountAllByQues INNER JOIN qryCountRespPerQues ON qryCountAllByQues.fkQuestionID = qryCountRespPerQues.fkQuestionID;


The query above will show only those responses that are used, but if none of your respondants choose the value 1 for a particular question, it will not show up since the count was =0.

Brianwarnock
01-25-2010, 11:39 AM
This can be done in 1 query using the sum if approach.
assume that your field is field1

SELECT Count(*) AS Expr5, Sum(IIf([field1]=1,1,0)) AS Expr1, Sum(IIf([field1]=2,1,0)) AS Expr2, Sum(IIf([field1]=3,1,0)) AS Expr3, Sum(IIf([field1]=4,1,0)) AS Expr4, [expr1]/[expr5] AS p1, [expr2]/[expr5] AS p2, [expr3]/[expr5] AS p3, [expr4]/[expr5] AS p4
FROM Table1

Count(*) counts the totals number of records thus null responses are counted, use count(field1) to only count the record if there is a response.

You can choose your own Aliases instead of Expr1 etc

Brian