Find average on the basis of corresponding duplicate VARCHAR values in MySQL


Let us first create a table −

mysql> create table DemoTable(
   Value int,
   Value2 varchar(100)
);
Query OK, 0 rows affected (0.84 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values(10,'999.999.999.999');
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable values(20,'888.888.888.888');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable values(30,'999.999.999.999');
Query OK, 1 row affected (0.09 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+-------+-----------------+
| Value | Value2          |
+-------+-----------------+
| 10    | 999.999.999.999 |
| 20    | 888.888.888.888 |
| 30    | 999.999.999.999 |
+-------+-----------------+
3 rows in set (0.00 sec)

Here is the query to find average on the basis of corresponding VARCHAR records::

mysql> select Value2,avg(Value) from DemoTable group by Value2;

This will produce the following output −

+-----------------+------------+
| Value2          | avg(Value) |
+-----------------+------------+
| 999.999.999.999 | 20.0000    |
| 888.888.888.888 | 20.0000    |
+-----------------+------------+
2 rows in set (0.06 sec)

Updated on: 27-Sep-2019

65 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements