Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Average of Average in one row

Tags:

sql

mysql

I'm not sure if this is possible what I'm trying to achieve. I want to get the avg of averaged columns.

SELECT avg(col1), avg(col2), avg(col3) FROM tbl

My Result should be the avg of all three avg columns, is this possible? Something like this

SELECT avg( col1, col2, col3) FROM tbl

doesn't work at MySQL 5.1

like image 346
onigunn Avatar asked Apr 06 '10 09:04

onigunn


People also ask

How do I average each row in Excel?

AutoSum lets you find the average in a column or row of numbers where there are no blank cells. Click a cell below the column or to the right of the row of the numbers for which you want to find the average. On the HOME tab, click the arrow next to AutoSum > Average, and then press Enter.

Can you average an average?

There is a common question that crops up in analytics, which is can you average your averages. The short answer is no, but a longer explanation is probably needed. Whether you have grouped your data by month, or region, or some other facet – each average you see is based on a different number of data points.

How do you average a row in SQL?

For example, 2+4+4+6+6+8 is 30 divided 6 which results in an average of 5. This is the basic syntax for the AVG function: SELECT AVG(column_name) FROM table_name; In this example, we have a table called students , with columns of id , name , date , and scores .

How do I calculate the average?

The average of a set of numbers is simply the sum of the numbers divided by the total number of values in the set. For example, suppose we want the average of 24 , 55 , 17 , 87 and 100 . Simply find the sum of the numbers: 24 + 55 + 17 + 87 + 100 = 283 and divide by 5 to get 56.6 .


2 Answers

Did you try:

SELECT avg( col1 + col2 + col3)/3.0 FROM tbl

You must check that there are no nulls in this columns.

like image 106
zendar Avatar answered Oct 19 '22 00:10

zendar


SELECT (AVG(col1) * COUNT(col1) +
        AVG(col2) * COUNT(col2) +
        AVG(col3) * COUNT(col3)) /
       (COUNT(col1) + COUNT(col2) + COUNT(col3))
FROM tbl
like image 39
Anthony Faull Avatar answered Oct 18 '22 22:10

Anthony Faull