# Getting Averages for 5 Columns

Thread began 7/23/2015 7:34 am by salaroche | Last modified 7/27/2015 8:07 am by Ray Borduin | 1473 views | 6 replies |

## 7/23/2015 7:34 am  |  #1 salaroche

### Getting Averages for 5 Columns

Hi:

I’m trying to get the average of numerical entries in 5 different columns. Please note that I’m NOT talking about Rows, but columns. For example: how to get the AVG for columns A, B, and C.

A B C
Mon 23 34 36
Tues 56 58 73
Wed 78 12 25
Thur 88 45 68
Fri 67 32 18
AVG

I’ve been experimenting with the function AVG() but thus far no success.

Any suggestions?

## 7/23/2015 1:01 pm  |  #2 Ray BorduinWebAssist

You can do simple math in a SQL statement without using a funciton:

SELECT (A+B+C/3) AS averageVal FROM table

Did this help? Tips are appreciated...

## 7/23/2015 4:31 pm  |  #3 salaroche

Ray:

Your formula would give a “horizontal” average, meaning a ROW average, but the need is for a “vertical” average, meaning the average of all the entries in each column (NOT across columns)

In my example, the task would be to get the (“vertical”) average of column A, the average of column B, and the average of column C (NOT the average of column A+B+C)

Any suggestions?

## 7/23/2015 4:45 pm  |  #4 Ray BorduinWebAssist

For that you would need to use the AVG() function.

SELECT Avg(A) as AAverage, Avg(B) as BAverage FROM Table

Did this help? Tips are appreciated...

## 7/24/2015 8:52 am  |  #5 salaroche

Ray:

Maybe the context in which I’m using the AVG() function is not the appropriate one, but I’m getting an error message saying: Unknown column ‘cwAverage’ in field list.

This is the SELECT I’m using:

SELECT oxadmain.name , oxadmain.year , oxadmain.photoID , oxadmain.thumbs , stuperfdaily.ID , stuperfdaily.stuid , stuperfdaily.date , stuperfdaily.subject , stuperfdaily.teacher , stuperfdaily.cw , stuperfdaily.hw , stuperfdaily.con , stuperfdaily.par , stuperfdaily.att, AVG(cw) as cwAverage, AVG(hw) as hwAverage, AVG(con) as conAverage, AVG(par) as parAverage, AVG(att) as attAverage, ROUND((cwAverage+hwAverage+conAverage+parAverage+attAverage)/5, 2) AS avg FROM stuperfdaily INNER JOIN oxadmain ON stuperfdaily.stuid=oxadmain.stuid GROUP BY stuperfdaily.stuid

Any suggestions?

Just in case, attached please find a copy of the page in question.

Attached Files
-stuperf_PeriodResults.php

## 7/24/2015 5:06 pm  |  #6 Ray BorduinWebAssist

You can't refer to the calculated values later in the select statement like you are in the ROUND() function. You would have to use the AVG() function again in there like:

SELECT oxadmain.name , oxadmain.year , oxadmain.photoID , oxadmain.thumbs , stuperfdaily.ID , stuperfdaily.stuid , stuperfdaily.date , stuperfdaily.subject , stuperfdaily.teacher , stuperfdaily.cw , stuperfdaily.hw , stuperfdaily.con , stuperfdaily.par , stuperfdaily.att, AVG(cw) as cwAverage, AVG(hw) as hwAverage, AVG(con) as conAverage, AVG(par) as parAverage, AVG(att) as attAverage, ROUND((AVG(cw)+AVG(hw)+AVG(con)+AVG(par)+AVG(att))/5, 2) AS avg FROM stuperfdaily INNER JOIN oxadmain ON stuperfdaily.stuid=oxadmain.stuid GROUP BY stuperfdaily.stuid

Did this help? Tips are appreciated...

Ray:

## Build websites with a little help from your friends

Your friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.

## Build websites from already-built web applications

These out-of-the-box solutions provide you proven, tested applications that can be up and running now.  Build a store, a gallery, or a web-based email solution.

## Everything else!

Close Window

Account or customer service questions?

Need technical support?

Content

Layout

Ease of use

We do not respond to comments submitted from this page directly, but we do read and analyze any feedback and will use it to help make your experience better in the future.

Close Window

## We were unable to retrieve the attached file

Close Window

### Attach and remove files

• Text Formatting
• Insert Element
• Block Formatting
• Cancel
Close Window

Close Window

#### This is how you use right click RTF editing

Enable right click RTF editing option allows you to add html markup into your tutorial such as images, bulleted lists, files and more...

-- click to close --