关于SQL Server中SQL语句Round和Sum函数的问题(50分)

  • 主题发起人 主题发起人 DragonInCity
  • 开始时间 开始时间
D

DragonInCity

Unregistered / Unconfirmed
GUEST, unregistred user!
select BillNo,<br>Profit=Sum(Convert(Money,Round(Isnull(SettleFy,0)*Isnull(SettleRate,0),2))),<br>Profit1=Round(Convert(Money,Sum(Isnull(SettleFy,0)*Isnull(SettleRate,0))),2)<br>from chgsettle where SettleNo='JSD8608070062' <br>group by BillNo<br>order by billno<br>=====================<br>BillNo &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Profit &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Profit1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>-------------------- --------------------- --------------------- <br>D86308070103 &nbsp; &nbsp; &nbsp; &nbsp; 1290.4300 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1290.4300<br>D86308070104 &nbsp; &nbsp; &nbsp; &nbsp; 2169.0300 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2169.0200<br>D86308070105 &nbsp; &nbsp; &nbsp; &nbsp; 2169.0300 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2169.0200<br>D86308070106 &nbsp; &nbsp; &nbsp; &nbsp; 2169.0300 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2169.0200<br>D86308070107 &nbsp; &nbsp; &nbsp; &nbsp; 2169.0300 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2169.0200<br>(所影响的行数为 5 行)<br>Profit=Sum(Convert(Money,Round(Isnull(SettleFy,0)*Isnull(SettleRate,0),2))),<br>--先保留小数点后两位小数,然后再汇总<br>Profit1=Round(Convert(Money,Sum(Isnull(SettleFy,0)*Isnull(SettleRate,0))),2)<br>--先汇总,然后再保留小数点后两位小数<br><br>按正常来说,应该是后一种写法产生的结果大,前一种产生的结果小;可实际执行的结果却是反过来了,请哪位大侠给解释一下这是为什么,谢谢了!
 
假设每次乘机出来的结果都是1.544,看下面简化例子<br>sum(round(1.544)) &lt; round(sum(1.544)) &nbsp;这个就是这里产生的原因<br>而<br>sum(round(1.555)) &gt; round(sum(1.555) ,不知道楼主有没有看明白
 
这是数据问题,就是四舍五入后再汇总还是汇总四舍五入后那个大的。<br>1.0443<br>1.0442<br>第三位四舍五入后汇总2.08<br>第三位汇总后四舍五入2.09<br><br>1.0463<br>1.0482<br>第三位四舍五入后汇总2.10<br>第三位汇总后四舍五入2.09
 
后退
顶部