Solved

Help with adding percent of total in Access Query

Posted on 2008-09-29
9
644 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

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