从数学上讲,这些结果是相似的,我只是对它们的行为感到好奇。当我的局部变量被声明为浮点数的第一个版本时,舍入函数会停止显示在第二个小数处的小数。在将局部变量声明为 int 的第二个版本中,round
函数四舍五入到小数点后两位,然后添加一堆零。为什么变量类型会导致round
函数表现不同?
declare @totalpop float
set @totalpop = (select count(distinct patid) from members)
select @totalpop
select edutext
,COUNT(*) as educationCounts
,round(100.0*COUNT(*)/@totalpop,2)
from
(
select distinct m.patid, e.eduText
from members as m
inner join EducationTable as e on e.eduID = m.education
)x
group by x.eduText
--不四舍五入到小数点后两位
declare @totalpop int
set @totalpop = (select count(distinct patid) from members)
select @totalpop
select edutext
,COUNT(*) as educationCounts
,round(100.0*COUNT(*)/@totalpop,2)
from
(
select distinct m.patid, e.eduText
from members as m
inner join EducationTable as e on e.eduID = m.education
)x
group by x.eduText