![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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)
Advertisements