Solved

# Average in a Query

Posted on 2007-03-21
254 Views
Hello Experts!

I am trying to calculate an average in a query, but I must be missing something in the syntax.

The table I'm working from has 7 columns - 1 "Name" column and 6 "Value" columns (V1, V2, etc).  Some of the value columns may be zero.  I have pulled all 7 columns into the query data fields.

My approach was to write an expression named "Average" in the 8th column in the query:
Average:  Avg([V1]+[V2]+[V3]+[V4]+[V5]+[V6])

Unfortunately, this simply sums the values.

Any ideas?
0
Question by:thotovec
• 3
• 3

LVL 142

Expert Comment

ID: 18765744
average function computes the average of all the grouped rows (if any).

in your case, you need to take the sum and divide by 6:
Average:  ([V1]+[V2]+[V3]+[V4]+[V5]+[V6])/6

if some values are zero, and you want to ignore them, you will have to "count" those.
however, there is no way of doing so that is works for all the databases with the same syntax, so you need to specify
0

Author Comment

ID: 18766187
Need to specify . . . ?
0

LVL 142

Expert Comment

ID: 18766234
what kind of database are you using?
I assume MS Access, but I want to avoid anyone guess around.
0

Author Comment

ID: 18767091
I'm sorry - MS Access 2003.
0

LVL 142

Accepted Solution

Guy Hengel [angelIII / a3] earned 500 total points
ID: 18767132
Average:  ([V1]+[V2]+[V3]+[V4]+[V5]+[V6])/ (
IIF( ISNULL(V1), 0, 1)  + IIF( ISNULL(V2), 0, 1) + IIF( ISNULL(V3), 0, 1 )  +
IIF( ISNULL(V4), 0, 1)  + IIF( ISNULL(V5), 0, 1) + IIF( ISNULL(V6), 0, 1 )
)
0

Author Comment

ID: 18767437
Perfect!  Thank you!
0

## Featured Post

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This video discusses moving either the default database or any database to a new volume.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.