
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 of corresponding records (Product Price) from duplicate product ids in MYSQL
For this, use AVG() for average and GROUP BY to group records of duplicate column (Product Id). Let us first create a table −
mysql> create table DemoTable1490 -> ( -> ProductId varchar(20), -> ProductPrice int -> ); Query OK, 0 rows affected (0.43 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1490 values('PRODUCT_100',700); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1490 values('PRODUCT_200',500); Query OK, 1 row affected (0.31 sec) mysql> insert into DemoTable1490 values('PRODUCT_200',1000); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1490 values('PRODUCT_100',1300); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1490 values('PRODUCT_200',300); Query OK, 1 row affected (0.14 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1490;
This will produce the following output −
+-------------+--------------+ | ProductId | ProductPrice | +-------------+--------------+ | PRODUCT_100 | 700 | | PRODUCT_200 | 500 | | PRODUCT_200 | 1000 | | PRODUCT_100 | 1300 | | PRODUCT_200 | 300 | +-------------+--------------+ 5 rows in set (0.00 sec)
Here is the query to find average −
mysql> select ProductId,avg(ProductPrice) from DemoTable1490 -> group by ProductId;
This will produce the following output −
+-------------+-------------------+ | ProductId | avg(ProductPrice) | +-------------+-------------------+ | PRODUCT_100 | 1000.0000 | | PRODUCT_200 | 600.0000 | +-------------+-------------------+ 2 rows in set (0.03 sec)
- Related Questions & Answers
- Display highest amount from corresponding duplicate ids in MySQL
- MongoDB aggregation to sum product price with similar IDs
- MySQL SELECT products WHERE 'average price per product' < value?
- SUM corresponding duplicate records in MySQL
- MySQL rows concatenation to fetch maximum corresponding value from duplicate IDs?
- Find average on the basis of corresponding duplicate VARCHAR values in MySQL
- Add records from corresponding duplicate values in another column with MySQL
- Finding the average and display the maximum average of duplicate ids?
- Get minimum value from a column (floating values) with corresponding duplicate ids in MySQL
- Select count of values (Yes, No) with same ids but different corresponding records in MySQL?
- Find and display duplicate records in MySQL?
- Python Program to find out the price of a product after a number of days
- Update table with duplicate ids in MySQL
- How to get a rating average in MongoDB based on duplicate ids?
- 8086 program to determine product of corresponding elements of two array elements
Advertisements