473,396 Members | 2,018 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,396 software developers and data experts.

To calculate an average for a multiple record query.

abouddan
Hi all
I am working on an accounting project using MS Access 2000, that demands to calculate many fields in a spesific record.
The problem:
The query I am using returns many records and for each record 3 fields, let's say Data1, Data2, Data3.
In the form related to this query I have 5 text boxes.The three text boxes are bound to the query. The two other text boxes are for displaying calculations,
the first one displays the sum of the three fields (Data1, Data2, Data3), and the other one displays their average.
Note that just the positif numbers (>0) are calculated.
Ex: Data1=10
Data2=0
Data3=20
the result:
textbox4=30 (10+20)
textbox5=15 ((10+20)/2)

What I need is: How can I assing the result of summation and the average to the text boxes for each record?
Feb 14 '07 #1
5 16214
MMcCarthy
14,534 Expert Mod 8TB
Try this ...

Expand|Select|Wrap|Line Numbers
  1. SELECT Data1, Data2, Data3, (Data1 + Data2 + Data3) As Total,
  2. IIf([Data1]=0, IIf([Data2]=0, IIf([Data3]=0,0),1,2),3) As AvgNum,
  3. IIf(AvgNum<>0,Total/AvgNum,0) As AvgTotal
  4. FROM TableName;
  5.  
Mary
Feb 14 '07 #2
NeoPa
32,556 Expert Mod 16PB
As Mary says, it is better to do the calculation in the Query itself. It is possible, though far more complex, to do it in the Record Source of the TextBoxes.
Feb 14 '07 #3
Try this ...

Expand|Select|Wrap|Line Numbers
  1. SELECT Data1, Data2, Data3, (Data1 + Data2 + Data3) As Total,
  2. IIf([Data1]=0, IIf([Data2]=0, IIf([Data3]=0,0),1,2),3) As AvgNum,
  3. IIf(AvgNum<>0,Total/AvgNum,0) As AvgTotal
  4. FROM TableName;
  5.  
Mary

Thanks Mary for your support but this is not what I need.
First: a syntax error occurs when running the code. I think the problem is in the second line near "1,2)". The ",2" is extra...
Second: in the example I gave I said that Data1=10 and Data2=0 and Data3=20
so the average must be (10+20)/2. Using your code the result is (10+30)/3, witch's wrong.
Third: If the first field "Data1" is <> 0, he is not going to test the second field nor the third.
But thank you because using IIF in query was new to me. So I built my own query using IIF and it worked very well.

Expand|Select|Wrap|Line Numbers
  1. Select Data1, Data2, Data3, (Data1+ Data2+ Data3) as Total, 
  2. IIF (Data1 =0, 0, 1) as D1,
  3. IIF (Data2 =0, 0, 1) as D2,
  4. IIF (Data3 =0, 0, 1) as D3,
  5. (D1+ D2+ D3) as Avg,
  6. IIF (Avg= 0, 0, Total/Avg) as Average)
  7. From tablename
Feb 15 '07 #4
MMcCarthy
14,534 Expert Mod 8TB
Thanks Mary for your support but this is not what I need.
First: a syntax error occurs when running the code. I think the problem is in the second line near "1,2)". The ",2" is extra...
Second: in the example I gave I said that Data1=10 and Data2=0 and Data3=20
so the average must be (10+20)/2. Using your code the result is (10+30)/3, witch's wrong.
Third: If the first field "Data1" is <> 0, he is not going to test the second field nor the third.
But thank you because using IIF in query was new to me. So I built my own query using IIF and it worked very well.

Select Data1, Data2, Data3, (Data1+ Data2+ Data3) as Total,
IIF (Data1 =0, 0, 1) as D1,
IIF (Data2 =0, 0, 1) as D2,
IIF (Data3 =0, 0, 1) as D3,
(D1+ D2+ D3) as Avg,
IIF (Avg= 0, 0, Total/Avg) as Average)
From tablename
Glad you got it working. My fault I hadn't tested the IIf statement properly.

Mayr
Feb 15 '07 #5
NeoPa
32,556 Expert Mod 16PB
You can tidy it up a little more as D1 through D3 are not required more than once :
Expand|Select|Wrap|Line Numbers
  1. SELECT Data1,Data2,Data3,
  2.        (Data1+Data2+Data3) as Total,
  3.        IIf(Data1=0,0,1)+IIf(Data2=0,0,1)+IIf(Data3=0,0,1) As Avg,
  4.        IIF (Avg=0,0,Total/Avg) as Average)
  5. FROM tablename
Feb 15 '07 #6

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

Similar topics

7
by: sql-db2-dba | last post by:
Does DB2 just fudge it when it is an empty table? Is there a "formula" for average row size when you have variable length records. Or you really have to know what your application is packing into...
3
by: Steven Stewart | last post by:
Hi there, I have posted about this before, but yet to arrive at a solution. I am going to try to explain this better. I have an Employees table and a Datarecords table (one-to-many...
2
by: Wayne Aprato | last post by:
I've read most, if not all, of the posts on moving average and still can't find a simple solution to my problem (if a simple solution exists!) I have a table with 2 fields: Hours and Injuries. I...
2
by: mscdex | last post by:
I have a server application that accepts file transfers (utilitzing tcplistener) and was wondering how I would efficiently go about determining the calculate transfer rate while I am transferring...
3
by: larry | last post by:
Hi, Is there a way to calculate the sum of the numbers in each field for each record in the recordset returned from the query? Do I have to use VBScript? Thanks, Larry
0
by: SuzK | last post by:
I am trying to calculate in VBA in Access 2002 a moving average and update a table with the calculations. Fields in my WeeklyData table are Week Ending (date) ItemNbr (double) Sales Dollars...
12
by: denveromlp | last post by:
Hello, I'm new to Access and trying to calculate a rolling 12 month average from some time data. Each data point is a date and a measurement taken at that date. As far as I can tell, the only...
5
FishVal
by: FishVal | last post by:
IMHO, the following is not a how-to-do instruction to solve a particular problem but more a concept-proof stuff demonstrating possibilities of SQL. So, let us say the problem is to calculate...
7
by: Wiredboy | last post by:
Hi, i'm pretty new in this and i hope somebody can help me with this: i have a db with 5 columns with different values and i want to calculate the average of them. I've tried to create a formula...
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?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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
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.