MySQL求百分率

報表業務要求求字段分類在表中百分率,SQL如下(SQL中表為本人業務表,須更改):
Mysql代碼 
SELECT biz_type,round((sumCate/sumCount)*100,2) as percent 
From (select biz_type, count(id) as sumCate from sta_report group by biz_type) as a, 
(select count(id) as sumCount from sta_report) as  b 
 
結果隻需加上“%”即可,已經乘以瞭“100”,且已經把結果截斷為隻含二位小數。
附SQL中取小數點後兩位語句:
select round(小數,2) from 表
select convert(decimal(18,2),round(小數,2)) from 表
select cast(小數 as numeric(10,2)) from 表

作者“憤怒的三米”
 

發佈留言