473,657 Members | 2,776 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to calculate the average of columns

6 New Member
Please, help me!
I have a DB (Access 2007) with 4 columns. The first is the ID of the patient and the others are indexes PB1 , PB2, PB3. I have difficulties to calculate the average of columns PB1, PB2, PB3, because when 2 fields are filled the average needs to be divided by 2. Zero is a value too.
Thanks!
Oct 7 '10 #1
10 3202
NeoPa
32,568 Recognized Expert Moderator MVP
So you want the average of all fields which have data in them (including zeroes).

In that case, try :
Expand|Select|Wrap|Line Numbers
  1. SELECT ID
  2.      , [PB1]
  3.      , [PB2]
  4.      , [PB3]
  5.      , IIf(IsNull([PB1] & [PB2] & [PB3]),
  6.            Null
  7.          , (Nz([PB1],0) + Nz([PB2],0) + Nz([PB3],0)) /
  8.            (IIf(IsNull([PB1]),0,1) +
  9.             IIf(IsNull([PB2]),0,1) +
  10.             IIf(IsNull([PB3]),0,1)) AS [Avg]
  11. FROM   [YourTable]
Welcome to Bytes!
Oct 7 '10 #2
Ilina Ivanova
6 New Member
Thank you, NeoPa,
I`ve tried it, but it is impossible to run the query because of message „ ... the query does not include the specified expression „ID” as a part of an aggregate function”.
I am a beginner in Access and probably I make a mistake.
Oct 8 '10 #3
NeoPa
32,568 Recognized Expert Moderator MVP
This is a message you would get if there were any aggregate calls in your SQL. There is none in what I posted, but there is a name ([Avg]) which could be misinterpreted if not copied in correctly. Why don't you post the actual SQL you're using and I'll look at it for you. Alternatively, try grabbing this again (from post #2) and copy/paste it in to your project exactly as it is. If that works then no need for further worry.
Oct 8 '10 #4
Ilina Ivanova
6 New Member
Sorry NeoPa. I`ve tried many times including copy/paste after your recommendation but without any result. The expression disappears after I run the query or a message of incorrect subquery`s syntax appears.
Is it possible to e-mail you the file for help me.
Oct 12 '10 #5
NeoPa
32,568 Recognized Expert Moderator MVP
No to email Ilina, but it's perfectly possible to attach a copy to a post in here which should suffice.

When attaching your work please follow the following steps first :
  1. Remove anything not relevant to the problem. This is not necessary in all circumstances but some databases can be very bulky and some things do not effect the actual problem at all.
  2. Likewise, not entirely necessary in all cases, but consider saving your database in a version not later than 2003 as many of our experts don't use Access 2007. Largely they don't want to, but some also don't have access to it. Personally I will wait until I'm forced to before using it.
  3. If the process depends on any linked tables then make local copies in your database to replace the linked tables.
  4. If the database includes any code, ensure that all modules are set to Option Explicit (See Require Variable Declaration).
  5. If you've done anything in steps 1 to 4 then make sure that the problem you're experiencing is still evident in the updated version.
  6. Compile the database (From the Visual Basic Editor select Debug / Compile {Project Name}).
  7. Compact the database (Tools / Database Utilities / Compact and Repair Database...).
  8. Compress the database into a ZIP file.
  9. When posting, scroll down the page and select Manage Attachments (Pressing on that leads you to a page where you can add or remove your attachments. It also lists the maximum file sizes for each of the allowed file types.) and add this new ZIP file.
It's also a good idea to include some instructions that enable us to find the issue you'd like help with. Maybe some instructions of what to select, click on, enter etc that ensures we'll see what you see and have the same problems.
Oct 12 '10 #6
Ilina Ivanova
6 New Member
I have one table [Status] with four columns (ID, PB1, PB2, PB3) (the data in the other fields is not relevant to my question and I removed it). I want to create a query [Index] showing the ID in one column and the average of PB1,PB2,PB3 in second column. I have difficulties to build the expression for average because there are records with missing values and I need the sum to be divided by 2 or 1. Zero is a value too.
Attached Files
File Type: zip Focal_Infection2.zip (12.1 KB, 90 views)
Oct 13 '10 #7
NeoPa
32,568 Recognized Expert Moderator MVP
Ilina, you have not left in any of your attempts to paste in the code I suggested, so it will be hard for me to tell you where you went wrong.

Let me look at it and see what I can do for you.
Oct 14 '10 #8
NeoPa
32,568 Recognized Expert Moderator MVP
It seems I left a closing parenthesis (')') from the end of the suggested SQL. Not hard to do when working blind. Once I tried it the error message (different from any you posted) made it clear what the problem was and where. Here's the updated SQL :
Expand|Select|Wrap|Line Numbers
  1. SELECT ID
  2.      , [PB1]
  3.      , [PB2]
  4.      , [PB3]
  5.      , IIf(IsNull([PB1] & [PB2] & [PB3]),
  6.            Null
  7.          , (Nz([PB1],0) + Nz([PB2],0) + Nz([PB3],0)) /
  8.            (IIf(IsNull([PB1]),0,1) +
  9.             IIf(IsNull([PB2]),0,1) +
  10.             IIf(IsNull([PB3]),0,1))) AS [Avg]
  11. FROM   [Status]
Let me know if you still have problems getting it to work. It worked perfectly for me by the way (Your example data was perfect to test with).
Oct 14 '10 #9
Ilina Ivanova
6 New Member
Thank you, NeoPa! It`s really work! Thank you very, very much! It was so usefully joining the Bytes.com Expert Community!
Oct 18 '10 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

2
3639
by: Steve | last post by:
Hi all How would I find out the average date when given a bunch of dates? For example, I want to find the average length in time from the following dates: ---------------------------------------------------- Start Date End Date 01/01/2004 12:50pm 02/01/2004 18:40pm 02/01/2004 13:40pm 02/01/2004 13:57pm 02/01/2004 14:30pm 02/01/2004 19:50pm
1
2030
by: Alex | last post by:
I have a comma separated text file in the form: 11/28/2004 12:00:00, 2.78655354 11/28/2004 12:00:10, 1.9978 11/28/2004 12:00:20, 0.978 11/29/2004 12:00:00, 1.98467 11/29/2004 12:00:10, 3.005 I would like to be able to read the file and then write just the date and the average value to a to an array and then output it to a text
1
7284
by: Sedigh | last post by:
Hi Everybody, I need to write a macro on my Excel sheet to calculate the average of cells for me. This is the code I have written but the average function is not working. Can you please let me know how can I use the Average function in my code. Thanks alot Sub Macro1() Ncol = 1 nrowColumn2 = 1 i = 1 Do Until IsEmpty(Worksheets(1).Cells(i, 1))
7
10232
by: jamesnkk | last post by:
Hi, Although this question may not directly link to Access, but I think it a common question to those developer.so hope you could suggest a solution. How do I get the average cost for Item sold, so I could calculate the Profit.- Same Product item could bought from different suppliers, different suppliers have different cost price or buying price. There is only 1 standard selling price, In order to calculate the profit, I would take...
2
5274
by: julia1128 | last post by:
Hi all, I'm a newbie to perl, really need some help. It seemed to be a very simple question but I kept getting wrong answers... Thanks in advance!! The text file looks like this: 0.000004 56 78 0.000005 67 89 0.000006 78 23 0.000007 12 56
0
1458
by: pratikm23 | last post by:
add runtime textboxes and input values into them to calculate the average my code to add the runtime textboxes is: Private Sub Command1_Click() Dim TextControl As TextBox controlid = controlid + 1 Load Text1(controlid) Set TextControl = Text1(controlid)
0
8316
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8737
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8509
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7345
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6174
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4327
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2735
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1967
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.