[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Help with adding percent of total in Access Query

Posted on 2008-09-29
9
Medium Priority
?
652 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
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 

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
 
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 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Windows Explorer let you handle zip folders nearly as any other folder: Copy, move, change, and delete, etc. In VBA you can also handle normal files and folders, but zip folders takes a little more - and that you'll find here.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

830 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