MySQL - 多行总数的百分比

时间:2017-01-28 04:13:28

标签: mysql sql database

我得到了一个包含多个市场详细信息的Table,我希望在this这样的新列中获得总销售额的百分比。 我尝试了以下代码,但它不起作用。

Select Market, Sale, Sale*100/t.s as %total
from mytablename
cross join (select sum(Sale) as s from mytablename
group by Market

任何人都可以提供帮助吗?

由于

1 个答案:

答案 0 :(得分:2)

使用子查询和聚合函数:

Select t.Market, sum(t.Sale), sum(Sale)*100.0/total.s as `%total`
from mytablename t cross join
     (select sum(Sale) as s from mytablename) total
group by t.Market;