declare @ACount int
select @ACount=count(*) from 表 --计算总数,避免放在大数据的select中影响效率
if @ACount>0 --判断是否有记录,无记录时@Acount为0,不能做除数
select 表.name,cast(count(*) as numeric(10,3))/cast(@ACount as numeric(10,3)) from 表 group by 表.name --做百分比时要避免直接用/,会整除的,就无法判断%比例了.
else
select '表中无记录,无法统计比例'
--小数点后保留三位,换算成%比,应该如12.5%的精确度,numeric(10,3)前面的10如果长度不够可以根据需要修改