Jakob
Jakob

Reputation: 4854

Average of multiple averages

I'm running a sql query where I have some averages, now I'd like to have the average of those averages so

AVG(q1) as q1,
AVG(q2) as q2,
AVG(q3) as q3,
AVG(q4) as q4,

and then I don't know how to get the average of all averages as I can't do AVG(q1,q2,q3,q4) and AVG(q1+q2+q3+q4+q5) won't return me the total average across all rows and columns, but only the average of each row.

Upvotes: 0

Views: 4124

Answers (2)

Traxo
Traxo

Reputation: 19002

Yeah you can 'reuse' the values with another SELECT

SELECT a.*,(q1+q2+q3+q4)/n as avg_all FROM (
  SELECT AVG(q1) as q1,
         AVG(q2) as q2,
         AVG(q3) as q3,
         AVG(q4) as q4.... ) a

Upvotes: 1

dfb
dfb

Reputation: 13289

Easiest way is to compute it manually

(AVG(q1) + AVG(q2) ... + AVG(qn))/(n*1.0)

Upvotes: 3

Related Questions