sqlserver sum()后再除以10000后丢精度

一、修改前

select year,month,sum(ysl)ysl1,sum(isnull(ysl_sy_dx,0))ysl2,sum(isnull(actual_consume_total,0)) as ysl1_1,(sum(isnull(actual_consume_groundwater,0))) as ysl2_1
from(select re.xian_cd,year,month,sum(isnull(ysl,0)) ysl,sum(isnull(ysl_sy_dx,0))ysl_sy_dx,	
			cast((cast(sum(isnull(ysl,0)) as decimal(18,6)) / 10000) as decimal(18,6)) as actual_consume_total,
			cast((cast(sum(isnull(ysl_sy_dx,0)) as decimal(18,6)) / 10000) as decimal(18,6)) as actual_consume_groundwater
		from test
		group by xian_cd,year,month
)a
where year = 2023 and month = 2
group by year,month
order by year,month;

运行结果:

yearmonthysl1(立方米)ysl2(立方米)ysl1_1(万立方米)ysl2_1(万立方米)
202327464174.2872423411.559746.417352242.341079

二、修改后

select year,month,sum(ysl)ysl1,sum(isnull(ysl_sy_dx,0))ysl2,sum(isnull(actual_consume_total,0)) as ysl1_1,(sum(isnull(actual_consume_groundwater,0))) as ysl2_1
from(select re.xian_cd,year,month,sum(isnull(ysl,0)) ysl,sum(isnull(ysl_sy_dx,0))ysl_sy_dx,	
			cast((cast(sum(isnull(ysl,0)) as decimal(18,6)) / 10000.0) as decimal(18,6)) as actual_consume_total,
			cast((cast(sum(isnull(ysl_sy_dx,0)) as decimal(18,6)) / 10000.0) as decimal(18,6)) as actual_consume_groundwater
		from test
		group by xian_cd,year,month
)a
where year = 2023 and month = 2
group by year,month
order by year,month;

运行结果:

yearmonthysl1(立方米)ysl2(立方米)ysl1_1(万立方米)ysl2_1(万立方米)
202327864174.2874423411.559786.41742870442.34115590

三、总结

        从上面可以看出,ysl1和ysl1_1、ysl2和ysl2_1的整数部分是一样的,有区别的是小数位(转成万m³后要保留4位小数),第一种方法的差别比较大,第二种换算后就一样了

        区别有两点:

                ①cast(sum(isnull(ysl_sy_dx,0)) as decimal(18,6)),sum()之后县转换成decimal

                ②/10000改成/10000.0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值