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

 

   

Link to comment
Share on other sites

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".

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.