473,404 Members | 2,213 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

COUNTING YES/NO FIELDS

I have a query that returns the following sample data: Surname,
Baptised, Confirmed, Regular Communicant. The last 3 fields are
yes/no type. I want to report the number of "Yes" answers in each
field. I can see it easily enough on a report and add them up
manually, but can't think of a way of wording the query or a
calculated control within the report. Any suggestions, please? Is
there a (Count[fieldname]=Yes) type of construction? By the way, I'm
completely ignorant of VBA. Thanks for any help.
Nov 12 '05 #1
7 15043
Andrew,
Yes has a value of -1 and No has a value of 0
So you can just Sum all your Yes/No Fields like:
Sum = - (YesNoField1 + YesNoField2 + YesNoField3)

--
Hope this helps
Arno R
"Andrew UK" <an****@aptrainingonline.co.uk> schreef in bericht
news:98**************************@posting.google.c om...
I have a query that returns the following sample data: Surname,
Baptised, Confirmed, Regular Communicant. The last 3 fields are
yes/no type. I want to report the number of "Yes" answers in each
field. I can see it easily enough on a report and add them up
manually, but can't think of a way of wording the query or a
calculated control within the report. Any suggestions, please? Is
there a (Count[fieldname]=Yes) type of construction? By the way, I'm
completely ignorant of VBA. Thanks for any help.

Nov 12 '05 #2
an****@aptrainingonline.co.uk (Andrew UK) wrote in
news:98**************************@posting.google.c om:
I have a query that returns the following sample data:
Surname, Baptised, Confirmed, Regular Communicant. The last 3
fields are yes/no type. I want to report the number of "Yes"
answers in each field. I can see it easily enough on a report
and add them up manually, but can't think of a way of wording
the query or a calculated control within the report. Any
suggestions, please? Is there a (Count[fieldname]=Yes) type
of construction? By the way, I'm completely ignorant of VBA.
Thanks for any help.


Easy as 1,2,3.

Use the query design tool.

1) Once you see your query, just click on the Sigma (aka Funny Z
mark) on the toolbar. A new row will appear on your design grid,
labeled TOTAL. The data for each item on the grid will read "Group
By".
Delete your Surname field if you want to total on the whole
membership, or leave it as group by if you want family totals, and
change (double-click) to "Sum". Double-click again to get "avg" if
you want, or open the dropdown to see all the choices.

Save and run the query. It will return the sum of each column's yes
values with a negative sign in front of it.

2) To get rid of the minus signs, go back to design mode.
Add *-1 to the end of each of the three fields.

Access adds names like EXPR1, EXPR2, EXPR3, and a colon ":" to
separate it from the statement.

3) change each EXPRx to a maningful name, save and run your totals
query.

Bob Q

Nov 12 '05 #3
Andrew,
Paste this code in the SQL view of a query (substituting your column and
table names ofcourse) and run it

SELECT Sum( iif(A = -1,1,0)) as YesA,
Sum( iif(A = 0,1,0)) as NoA,
Sum( iif(B = -1,1,0)) as YesB,
Sum( iif(B = 0,1,0)) as NoB,
Sum( iif(C = -1,1,0)) as YesC,
Sum( iif(C = 0,1,0)) as NoC
FROM Table1
HS
"Andrew UK" <an****@aptrainingonline.co.uk> wrote in message
news:98**************************@posting.google.c om...
I have a query that returns the following sample data: Surname,
Baptised, Confirmed, Regular Communicant. The last 3 fields are
yes/no type. I want to report the number of "Yes" answers in each
field. I can see it easily enough on a report and add them up
manually, but can't think of a way of wording the query or a
calculated control within the report. Any suggestions, please? Is
there a (Count[fieldname]=Yes) type of construction? By the way, I'm
completely ignorant of VBA. Thanks for any help.

Nov 12 '05 #4
Thanks to both Arno and Bob. I'll try your suggestions and let you
know how I get on!

Andrew
Nov 12 '05 #5
OK, I've tried your suggestions. Again, thanks to both of you. I am
so stupid!!! I had remembered that Yes is stored as -1 and No as 0 but
somehow hadn't made the leap. I was trying to Count them (thinking of
them as some kind of text field), when obviously Sum works perfectly!

I'm so pleased to have found this site. It could definitely make life
easier.

Andrew
Nov 12 '05 #6
On Thu, 25 Sep 2003 03:49:23 GMT in comp.databases.ms-access, "HSalim"
<HS****@msn.com> wrote:
Andrew,
Paste this code in the SQL view of a query (substituting your column and
table names ofcourse) and run it

SELECT Sum( iif(A = -1,1,0)) as YesA,
Sum( iif(A = 0,1,0)) as NoA,
Sum( iif(B = -1,1,0)) as YesB,
Sum( iif(B = 0,1,0)) as NoB,
Sum( iif(C = -1,1,0)) as YesC,
Sum( iif(C = 0,1,0)) as NoC
FROM Table1


I haven't tested for speed but the following may work faster
(definately can be typed in faster) than the IIf() functions:

For Access, where Yes is always -1: Sum(a)*-1

For portable code, e.g. maybe upsizing to SQL Server later on where a
Yes will be 1: Sum(Abs(a))

So either the Abs() or the *-1 will net you a 1, to count zeros just
use sum(1 - abs(a)) or Sum(1-a*-1) or simpler Sum(Not A)

--
A)bort, R)etry, I)nfluence with large hammer.
Nov 12 '05 #7
Trevor,
You are right, and it is a neater solution and portable.
Select Abs(Sum(A)) as YesA,
Abs(Sum(Not A)) as NoA,
Abs(Sum(B)) as YesB,
Abs(Sum(Not B)) as NoB,
Abs(Sum(C)) as YesC,
Abs(Sum(Not C)) as NoC
from Table1
"Trevor Best" <bouncer@localhost> wrote in message
news:vt********************************@4ax.com...
On Thu, 25 Sep 2003 03:49:23 GMT in comp.databases.ms-access, "HSalim"
<HS****@msn.com> wrote:
Andrew,
Paste this code in the SQL view of a query (substituting your column and
table names ofcourse) and run it

SELECT Sum( iif(A = -1,1,0)) as YesA,
Sum( iif(A = 0,1,0)) as NoA,
Sum( iif(B = -1,1,0)) as YesB,
Sum( iif(B = 0,1,0)) as NoB,
Sum( iif(C = -1,1,0)) as YesC,
Sum( iif(C = 0,1,0)) as NoC
FROM Table1


I haven't tested for speed but the following may work faster
(definately can be typed in faster) than the IIf() functions:

For Access, where Yes is always -1: Sum(a)*-1

For portable code, e.g. maybe upsizing to SQL Server later on where a
Yes will be 1: Sum(Abs(a))

So either the Abs() or the *-1 will net you a 1, to count zeros just
use sum(1 - abs(a)) or Sum(1-a*-1) or simpler Sum(Not A)

--
A)bort, R)etry, I)nfluence with large hammer.

Nov 12 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Chad Reid | last post by:
Hello, I have a bit of code that obviously doesn't work, but I need help creating a query that would have the same effect as if this query was working. SELECT * FROM (SELECT Count(*) AS...
2
by: N. Graves | last post by:
I have a table with records of games played with a field for players name and another fields is a "WinorLose". One player may play several games to complete a match. I want my report to tell me...
2
by: Glenn Cornish | last post by:
I have 5 fields in a table into which numbers between 1 and 45 can be entered. What I am having trouble with is being able to find out is how many times a particular number appears, regardless of...
3
by: Megan | last post by:
hi everybody- i'm having a counting problem i hope you guys and gals could give me some help with. i have a query that retrieves a bevy of information from several different tables. first let...
18
by: ChadDiesel | last post by:
I appreciate the help on this group. I know I've posted a lot here the last couple of weeks, but I was thrown into a database project at my work with very little Access experience. No other...
7
by: sathyashrayan | last post by:
Group, Following function will check weather a bit is set in the given variouble x. int bit_count(long x) { int n = 0; /* ** The loop will execute once for each bit of x set,
14
by: jpr | last post by:
Friends, I have a form with four fields, date1, date2, date3 and date4. All these have all a mm/dd/yyyy format and have their source to a table. I need to add an unbound control (I will name...
4
by: Dado | last post by:
I have a next situation with the textbox field: A - B = C 1. How to fill the A fill with the data from my previous recordset ? Can I do it with the expression builder ? 2. I want that every...
3
by: Nhd | last post by:
I have a question which involves reading from cin and counting the number of words read until the end of file(eof). The question is as follows: Words are delimited by white spaces (blanks,...
2
by: Viktor | last post by:
I have a problem with counting empty fields in table. For example: I have the field in which i coulld write several solutions. "done", "not done", "send", "not send" etc. In report i want to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.