以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.com/bbs/index.asp)
--  专家坐堂  (http://foxtable.com/bbs/list.asp?boardid=2)
----  想把SQL下面语句中,保留二位小数,老师应该怎么写?  (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=135107)

--  作者:xxfoxtable
--  发布时间:2019/5/17 8:13:00
--  想把SQL下面语句中,保留二位小数,老师应该怎么写?
(case when  b.金额 is null then 0 else b.金额 end)-(case when  b.实收金额 is null then 0 else b.实收金额 end)-(case when  b.优惠金额 is null then 0 else b.优惠金额 end)) as 余额,
--  作者:有点甜
--  发布时间:2019/5/17 9:19:00
--  

round(你的语句, 2) as 余额

 

https://www.cnblogs.com/yeagen/archive/2012/08/27/2658659.html

 


--  作者:xxfoxtable
--  发布时间:2019/5/17 9:30:00
--  

round((case when  b.金额 is null then 0 else b.金额 end)-(case when  b.实收金额 is null then 0 else b.实收金额 end)-(case when  b.优惠金额 is null then 0 else b.优惠金额 end)),2) as 余额

round(case when  b.金额 is null then 0 else b.金额 end)-(case when  b.实收金额 is null then 0 else b.实收金额 end)-(case when  b.优惠金额 is null then 0 else b.优惠金额 end),2) as 余额

这二种写法都不行


--  作者:有点甜
--  发布时间:2019/5/17 9:41:00
--  

1、不写round的时候,能否得到数值?

 

2、把代码在sqlserver数据库那里测试,得到什么?报什么错?


--  作者:xxfoxtable
--  发布时间:2019/5/17 10:07:00
--  
能得到数值,就是位数太多,在sqlserver数据库那里测试,就显示0,什么错误也不报
--  作者:有点甜
--  发布时间:2019/5/17 11:03:00
--  

参考这里写

 

https://www.cnblogs.com/yeagen/archive/2012/08/27/2658659.html

 

[此贴子已经被作者于2019/5/17 11:02:57编辑过]

--  作者:xxfoxtable
--  发布时间:2019/5/17 11:20:00
--  

cast(round((case when  b.金额 is null then 0 else 金额 end)-(case when  b.实收金额 is null then 0 else 实收金额 end)-(case when  b.优惠金额 is null then 0 else b.优惠金额 end),2) as numeric(20,2)) as 余额

我是这样写的,结果还是0


--  作者:有点甜
--  发布时间:2019/5/17 12:24:00
--  

round((case when  b.金额 is null then 0.0 else 金额 end)-(case when  b.实收金额 is null then 0.0 else 实收金额 end)-(case when  b.优惠金额 is null then 0.0 else b.优惠金额 end),2) as 余额

 

如果还有问题,实例发上来测试


--  作者:xxfoxtable
--  发布时间:2019/5/17 12:30:00
--  
 下载信息  [文件大小:   下载次数: ]
图片点击可在新窗口打开查看点击浏览该文件:data.rar

语句在包里的文本文件里,示例数据是第16  17行
--  作者:有点甜
--  发布时间:2019/5/17 15:30:00
--  

1、语句没问题,数据也没有问题。你金额列的类型是整型,从单价*数量过来的数据本身就已经是错了的啊。

 

2、请重新计算金额列的值,再核对你的结果。