sql 2005 数据查询统计大数计算精确度不准确 不知道什么原因 有点费解
if object_id('tb') is not null
drop table tb
create table tb(type varchar(20),accessoryNo varchar(20),deviceNo varchar(10),lastTime int)
insert tb
select 'A','s10','z11',10 union all
select 'B','s10','z11',11 union all
select 'C','s11','z12',5 union all
select 'A','s10','z11',9 union all
select 'A','s10','z13',9 union all
select 'B','s10','z11',3
if object_id('fees') is not null
drop table fees
create table fees(id int,fee numeric(18, 3))
insert fees
select 1, 75188.580
go
declare @sql1 nvarchar(4000)
declare @count int
declare @sql nvarchar(4000)
set @sql1=N'select @a=sum(lastTime) from tb'
exec sp_executesql @sql1,N'@a int output',@count output
set @sql='select row_number() over(order by [type]) as [序号],[type] as [型号],accessoryNo as [零件号],sum(lastTime) as [持续时间],1.0*sum(lastTime)/'+cast(@count as varchar(20))+'*fee as [分配额
]'
set @sql=@sql+' from tb,fees where id = 1 group by type,accessoryNO,fee'
set @sql=@sql+' union all select '''' as 序号,''全部型别'' as [型号] ,''全部零件'' as [零件号],sum(lastTime) as [持续时间],1.0*sum(lastTime)/'+cast(@count as varchar(20))+'*fee as [分配额] from tb,fees group by fee'
exec(@sql)
drop table tb
drop table fees
--查询结果是
/*
(1 行受影响)
序号 型号 零件号 持续时间 分配额
-------------------- -------------------- -------------------- ----------- ---------------------------------------
1 A s10 28 44793.145403520 即75188.58/47*28=44793.196596
2 B s10 14 22396.572701760 即75188.58/47*14=22396.598297872
3 C s11 5 7998.711517560 即75188.58/47*5=7998.785106383
0 全部型别 全部零件 47 75188.580000000
(4 行受影响)
即后面是我用计算器计算的
*/
不知道为什么会有不精确的情况,只要是75188.580000000这个数值很大,小数点后的数据就不准确的,如果小的话就没有问题
...全文


疑难问题
