How to combine aggregate functions in MySQL?

北城以北 提交于 2019-11-27 15:09:58

You have to use subqueries:

  SELECT x.user, 
         AVG(x.cnt)
    FROM (SELECT user, COUNT(answer) AS cnt
            FROM surveyValues 
           WHERE study='a1' 
        GROUP BY user) x
GROUP BY x.user

You can't wrap an aggregate with another aggregate. You could wrap an analytic in an aggregate, if MySQL supported analytic/ranking/windowing functions...

yes - those all look reasonable.

Have you tried them and received unexpected results?

usually, i would expect that you also include the driving column in the select list:

SELECT question, avg(count(answer)) 
FROM surveyValues 
WHERE study='a1' 
GROUP BY question; 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!