How to combine aggregate functions in MySQL?

前端 未结 2 1197
北海茫月
北海茫月 2020-12-03 18:36

I\'m just learning MySQL - is there a way to combine (or nest) aggregate functions?

Given a query:

SELECT user, count(answer) FROM surveyValues WHER         


        
2条回答
  •  暖寄归人
    2020-12-03 19:02

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

提交回复
热议问题