MySQL:可以计算MAX(AVG(字段))吗?

时间:2010-04-12 23:31:39

标签: mysql average

我当前的查询内容为:

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

cat_id涉及不同的类别:1,2,3或4

我是否可以在每个类别中找到每个用户的最大平均值,而无需设置其他表格?对于每个user_id

,返回可能是4最大avg_rate

请访问以下链接,例如:

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

3 个答案:

答案 0 :(得分:4)

可能不是最有效的方式:

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

答案 1 :(得分:2)

SELECT s.user_id,s.cat_id,max(s.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) as s 
GROUP BY  s.user_id,s.cat_id

答案 2 :(得分:0)

选择entry_id,user_id,cat_id,AVG(等级)作为avg_rate 来自entry_rate GROUP BY entry_id 按avg_rate desc限制1排序;