Solved

Help with adding percent of total in Access Query

Posted on 2008-09-29
9
640 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
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.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

707 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

19 Experts available now in Live!

Get 1:1 Help Now