473,416 Members | 1,698 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,416 software developers and data experts.

How to calculate the average of columns

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 3181
NeoPa
32,556 Expert Mod 16PB
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
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,556 Expert Mod 16PB
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
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,556 Expert Mod 16PB
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
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, 89 views)
Oct 13 '10 #7
NeoPa
32,556 Expert Mod 16PB
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,556 Expert Mod 16PB
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
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
NeoPa
32,556 Expert Mod 16PB
A pleasure Ilina.

When I joined here a few years back one of the top Access Experts (PEB) was from Bulgaria like you. He doesn't post much any more though unfortunately.
Oct 19 '10 #11

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

Similar topics

2
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:...
1
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 ...
1
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...
7
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,...
2
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 ...
0
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 ...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.