Implement Harmonic mean and Quadratic mean in MySQL?


Let us first create a table −

mysql> create table DemoTable1606
   -> (
   -> Value int
   -> );
Query OK, 0 rows affected (0.55 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1606 values(5);
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable1606 values(10);
Query OK, 1 row affected (0.12 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1606;

This will produce the following output −

+-------+
| Value |
+-------+
|     5 |
|    10 |
+-------+
2 rows in set (0.00 sec)

Here is the query to implement Harmonic mean and Quadratic mean in MySQL −

mysql> select (count(*)/sum(1/Value)) as HarmonicMean,
   -> sqrt(sum(Value * Value)/count(*)) as QuadraticMean
   -> from DemoTable1606;

This will produce the following output −

+--------------+-------------------+
| HarmonicMean | QuadraticMean     |
+--------------+-------------------+
|       6.6667 | 7.905694150420948 |
+--------------+-------------------+
1 row in set (0.03 sec)

Updated on: 16-Dec-2019

85 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements