Get the Average of Average in a single MySQL row?


You can use aggregate function AVG() for this. Let us first create a table −

mysql> create table DemoTable
(
   Value1 int,
   Value2 int,
   Value3 int
);
Query OK, 0 rows affected (0.54 sec)

Insert records in the table using insert command −

mysql> insert into DemoTable values(10,20,30);
Query OK, 1 row affected (0.30 sec)
mysql> insert into DemoTable values(13,15,18);
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable values(21,31,41);
Query OK, 1 row affected (0.21 sec)

Display records from the table using select command −

mysql> select *from DemoTable;

This will produce the following output −

+--------+--------+--------+
| Value1 | Value2 | Value3 |
+--------+--------+--------+
| 10     | 20     | 30     |
| 13     | 15     | 18     |
| 21     | 31     | 41     |
+--------+--------+--------+
3 rows in set (0.00 sec)

Following is the query to display average of average in a single row −

mysql> select avg(Value1+Value2+Value3)/3.0 from DemoTable;

This will produce the following output −

+-------------------------------+
| avg(Value1+Value2+Value3)/3.0 |
+-------------------------------+
| 22.11111111                   |
+-------------------------------+
1 row in set (0.00 sec)

Updated on: 30-Jul-2019

173 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements