如何根据mysql中的值获取平均值

时间:2015-03-10 13:05:28

标签: mysql

根据他们的价值,我试图获取数据结果及其平均值,例如:

select sum(rank) as value,area from temp GROUP BY area
value | Area
------+-------
15    | East
54    | North
49    | South
35    | West

但我希望他们的总平均值

value | Area  | Average
------+-------+---------
49    | East  | 40
15    | North | 40
56    | South | 40
40    | West  | 40

我怎么能得到这些?请帮忙

2 个答案:

答案 0 :(得分:1)

您可以使用子查询。这会给你想要的结果吗?

select x.value, t.area
from temp t cross join
     (select sum(rank) / count(distinct area) as value from temp) x
group by t.area, x.value;

答案 1 :(得分:0)

select sum(rank) as value, area, avg(value) from temp GROUP BY area