Solved

Help with adding percent of total in Access Query

Posted on 2008-09-29
9
643 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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
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.

809 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