Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL - Average of Unique rows

Tags:

sql

php

mysql

If I have three columns:

id, user, points

My data is:

+-------+------------------+-------------+
|   id  |       user       |  points     |
+-------+------------------+-------------+
|   1   |       A          |    100      |
+-------+------------------+-------------+
|   1   |       A          |    200      |
+-------+------------------+-------------+
|   2   |       B          |    300      |
+-------+------------------+-------------+
|   2   |       B          |    400      |
+-------+------------------+-------------+

I would like to have the average of ONLY the max points of each user. For this exmple I want to get as results: 300 points ((200+400)/2).

When I use the following Mysql query, I get: 250:

SELECT avg(points) FROM table
like image 577
Shamss Zaidan Avatar asked Nov 27 '25 18:11

Shamss Zaidan


1 Answers

SQL DEMO

Try this :

SELECT avg(points) FROM (
    SELECT max(points) as points FROM table1 group by id
) as T

Firstly get the max points of each user and then get the AVG from them.

like image 195
Md. Suman Kabir Avatar answered Nov 30 '25 08:11

Md. Suman Kabir



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!