Jump to content

[SOLVED] SQL AVG of values with common value in same table


hnumar7

Recommended Posts

I hope I can explain this well enough for someone to offer me some help.

 

I have a table called rate which has values 1-5 and an ID

 

the table looks like this:

 

rated - ID

 

5        10

4        10

3        8

2        8

 

I am attempting to try and get the highest average of any ID based on the values in column rated

 

so in the above example ID 10 would have an average of 4.5  and ID 8 would have an average of 2.5  ,  so ID 10 would be have the highest average and ID 8 would have the lowest average.

 

Does anyone know what my select statement or php code should look like to accomplish this?  I am somewhat comfortable with PHP. Any help or suggestions would be very much appreciated

 

Thanks

 

Henry

 

   

If I get you correctly, to get ID with highest AVG use this query:

SELECT
  ID
  , AVG(rated) avgRate
FROM rate
GROUP BY
  ID
ORDER BY
  avgRate DESC
LIMIT 1
;

 

To get the lowest, change "avgRate DESC" to "avgRate ASC".

 

 

Thanks so much.... I knew this was easy just couldn't figure it out!!... Thanks again for helping out a Newbie

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.