database - Weighted average calculation in MySQL? -


i using following query numbers:

select gid, count(gid), (select cou size gid = infor.gid)        infor  id==4325  group gid; 

the output getting @ current stage following:

+----------+-----------------+---------------------------------------------------------------+ | gid      | count(gid)      | (select gid size gid=infor.gid)                    | +----------+-----------------+---------------------------------------------------------------+ |       19 |               1 |                                                            19 |  |       27 |               4 |                                                            27 |  |      556 |               1 |                                                           556 |  +----------+-----------------+---------------------------------------------------------------+ 

i trying calculate weighted average i.e.

(1*19+4*27+1*556)/(19+27+556)

is there way using single query?

use:

select sum(x.num * x.gid) / sum(x.cou)   (select i.gid,                count(i.gid) num,                s.cou           infor      left join size s on s.gid = i.gid          i.id = 4325       group i.gid) x 

Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -