Get the Average of Average in a single MySQL row?

MySQLMySQLi Database

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)
raja
Published on 30-Apr-2019 10:01:36
Advertisements