如何计算sql中列Total的百分比

时间:2015-01-17 12:01:10

标签: php jquery mysql sql

RL   SumAmount      RL     SumAmount
Alex    120         Alex    24.00%  
Jonny   122         Jonny   24.40%  
Jack    13          Jack    2.60%   
David   125         David   25.00%  
Mike    120         Mike    24.00%  

总计500总计100.00%

我试图使用sql计算列总计百分比,如下面的屏幕截图但是遇到问题

1 个答案:

答案 0 :(得分:0)

获得比率:

select t.*, sumamount/tot.total
from table t cross join
     (select sum(sumamount) as total from table t) tot;

如果您希望将其表示为百分比:

select t.*, concat(format(sumamount/tot.total, 2), '%')
from table t cross join
     (select sum(sumamount) as total from table t) tot;