Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL: Is it possible to compute MAX( AVG (field) )?

Tags:

mysql

average

My current query reads:

SELECT entry_id, user_id, cat_id, AVG( rating ) as avg_rate
FROM `entry_rate`
WHERE 1
GROUP BY entry_id

cat_id relates to different categories: 1, 2, 3 or 4

Is there a way I can find the maximum average for each user in each category without setting up an additional table? The return could potentially be 4 maximum avg_rate for each user_id

Visit the link below for example:

http://lh5.ggpht.com/_rvDQuhTddnc/S8Os_77qR9I/AAAAAAAAA2M/IPmzNeYjfCA/s800/table1.jpg

like image 375
Brad Avatar asked Dec 23 '22 04:12

Brad


1 Answers

May not be the most efficient way:

select user_id, cat_id, MAX(avg_rate)
FROM (
    SELECT entry_id, user_id, cat_id, AVG( rating ) as avg_rate
    FROM entry_rate
    GROUP BY entry_id, user_id, cat_id) t
GROUP BY user_id, cat_id
like image 135
tloflin Avatar answered Jan 08 '23 19:01

tloflin