Solved

Help with adding percent of total in Access Query

Posted on 2008-09-29
9
642 Views
Last Modified: 2008-10-06
I need to add another column to my query that will show percent of total for groupings chosen below in sex (i.e. Male, Female and No response). It also need to account for the fact that there could cases where there are zeros (i.e. no male respondents to the questionnaire).
SELECT qryResults.sex, Count(qryResults.sex) AS CountOfsex

FROM qryResults

GROUP BY qryResults.sex

ORDER BY Count(qryResults.sex) DESC;

Open in new window

0
Comment
Question by:mamadouthiam
  • 5
  • 4
9 Comments
 
LVL 18

Expert Comment

by:David Robitaille
ID: 22596505
the problem is havint the totaL, you could achive this in 2 ways:
but do you need to have this in separate rows?
if yes then
SELECT qryResults.sex, Count(qryResults.sex) AS CountOfsex , Count(qryResults.sex)/total.total * 100 AS percentOfsex FROM qryResults, (select  Count(*) as total  FROM qryResults) as total GROUP BY qryResults.sex ORDER BY Count(qryResults.sex) DESC;  
to get all cases where there are zeros, you need to left join with a table that list all the possible responses. ex
 possibility
left join qryResults on
 possibility.item = qryResults.sex
0
 

Author Comment

by:mamadouthiam
ID: 22596733
davrob60,

I ran your query and got the following:

You tried to execute a query that does not include the specified expression 'Count(qryResults.sex)/total.total * 100' as part of an aggregate function.

Ideas?

0
 
LVL 18

Expert Comment

by:David Robitaille
ID: 22596757
put "count(qryResults.sex)/total.total * 100" in the group by clause
0
 

Author Comment

by:mamadouthiam
ID: 22597001
davrob60,


Sorry to be a pest but can you show exactly where and how... Not so good at SQL syntax

Much appreciated

mama
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 18

Expert Comment

by:David Robitaille
ID: 22597032

SELECT qryResults.sex, Count(qryResults.sex) AS CountOfsex , Count(qryResults.sex)/total.total * 100 AS percentOfsex FROM qryResults, (select  Count(*) as total  FROM qryResults) as total GROUP BY qryResults.sex, count(qryResults.sex)/total.total * 100 ORDER BY Count(qryResults.sex) DESC;  
0
 

Author Comment

by:mamadouthiam
ID: 22597622
I get an error that cannot have aggregate function in GROUP BY clause (count(qryResults.sex)/total.total * 100)
0
 
LVL 18

Accepted Solution

by:
David Robitaille earned 500 total points
ID: 22597745
sorry...
should be better..
again for the null, do you have a table with all the possible answers?
i like to use a table with code/description columns like "M"="Male"

SELECT qryResults.sex, Count(*) AS CountOfsex , (Count(*)*100.0 / total.total) AS percentOfsex 

FROM qryResults, 

(select  Count(*) as total  FROM qryResults) as total 

GROUP BY total.total, qryResults.sex ORDER BY Count(*) DESC;

Open in new window

0
 

Author Comment

by:mamadouthiam
ID: 22598184
I do have a table for all answers

Ok, I ran the query and it produced results. However when I try to add a round function, the forms that display the data break and say they can't find the qry. The query works the 1st time when I run it in SQl view and process, but something about rerunning via the form causes issues.

Any ideas what this could be?

mama
0
 
LVL 18

Expert Comment

by:David Robitaille
ID: 22598268
>I do have a table for all answers
then just "left join" it to retrive all answers for the 0% case.
<But something about rerunning via the form causes issues.
 What kind of "issue" could you be more specific?
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now