1、取2位小數
方式一 :
select truncate(data/total,2)
實際使用中發現丟失精度
方式二 :
select convert(data/total,decimal(10,2))
推薦此方法,數據準確
2、拼接%
select concat( truncate(1/3,2)*100 ,'%')
3、查詢結果加序號
select (@i:=@i+1) i ,data ,total from mytable ,(SELECT @i:=0) as i
4、整理SQL
select
(@i:=@i+1) i,
data,
total,
concat(convert((data/total)*100,decimal(10,2)),'%') as rate
from
( select
data,
sum(data) total,
from mytable
group by mytype order by data desc
) temp,
(SELECT @i:=0) as i