如何将分数转换为2小数点?

时间:2015-01-20 09:59:08

标签: sql

我想要计算百分比。以下给出的查询是计算百分比。 SQL:

select TeamName, Count1,Count0,
(count1*100.0)/nullif(Max(Count1) over(),0) Percentage1,
(count0*100.0)/nullif(max(Count1) over(),0) Percentage2
from tbl_Percentage

它在网格视图中显示结果如下:

TeamName   Count1   Count0    Percentage1      Percentage0
--------   -----    ------    -----------      -----------
Team1        1        2        33.3333333%     66.66666666%
Team2        3        0        100.000000%     0.000000000%
Team3        1        1        33.3333333%     33.33333333% 

我想将分数减少到3个小数点。如下图所示:

TeamName   Count1   Count0  Percentage1  Percentage0
--------   -----    ------  -----------  -----------
Team1        1        2        33.33%       66.66%
Team2        3        0        100.00%      0.00%
Team3        1        1        33.33%       33.33% 

帮我找到合适的解决方案。谢谢。

1 个答案:

答案 0 :(得分:1)

试试这个

select TeamName, Count1,Count0,
Cast((count1*100.0)/nullif(Max(Count1) over(),0) as decimal(9,3)) Percentage1,
 Cast((count0*100.0)/nullif(max(Count1) over(),0),3)as decimal(9,3))  Percentage2
from tbl_Percentage