简体   繁体   中英

how to calculate percentage in column Total in 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%  

Grand Total 500 Grand Total 100.00%

I was trying to calculate percentage in column Total using sql like below screen shot but getting problem

To get the ratio:

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

If you want this expressed as the percentage:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM