34,575
社区成员
发帖
与我相关
我的任务
分享
declare @n1 numeric(28,8), @n2 numeric(28,8),@n3 numeric(28,8)
select @n1=1.0 ,@n2=0.0002046
select @n3=@n1*@n2
select @n3
/*
result:
@n3= 0.00020500 (而不是0.0002046 因為小數位被截斷,只取到6位小數)
*/
--原因:
declare @n4 sql_variant
select @n4=@n1*@n2
select Type=SQL_Variant_property(@n4,'BaseType'),
[Precision]=SQL_Variant_property(@n4,'Precision'),
[scale]=SQL_Variant_property(@n4,'scale')
/*
小數位被截斷了。
type precision scale
--------------------------
numeric 38 6
*/
create table #temp
(
[operatorid] [int] NULL ,
[storehouseid] [int] NULL ,
[productid] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[summoney] [decimal](18, 9) NULL , --精度 4 改成6 翁
[sumamount] [decimal](18, 6) NULL ,--精度 4 改成6 翁
[price] [decimal](18,9) NULL --精度 4 改成6 翁
)
insert #temp(productid,storehouseid,sumamount,summoney,price)
select productid,@houseid,sum(amount) as amount,sum(summoney) as summoney ,
case when sum(amount)=0 then 0 else cast(sum(summoney)/sum(amount) as decimal(18,6)) end as Price
from
(
select itemid,productid,amount,summoney from p_productAccountSumTab where itemid=@endid and houseid=@houseid
union all
select '1',productid,sum(amount) as amount,sum(1.00000000*money) as money from kc_billjoinmaintab a
join kc_billjoinlisttab b on a.billdocument_id=b.billdocument_id
where operatedate>=@stdt and operatedate<@eddte+1 and storehouseid=@houseid group by productid
) a
group by productid
select cast(63883.63 as decimal(18,7))/23625
/*
---------------------------------------
2.7040689947089
(1 row(s) affected)
*/
--方式1计算结果
declare @returnvalue decimal(18,6),
@startamount decimal(18,6),
@startmoney decimal(18,6),
@inamount decimal(18,6),
@inmoney decimal(18,6)
if @startamount+@inamount<>0
set @returnvalue=ROUND((@startmoney+@inmoney)/(@startamount+@inamount),6)
--通过上面这种方式计算出来的结果,会按第7位小数四舍五入
--方式2计算结果
create table #temp
(
[operatorid] [int] NULL ,
[storehouseid] [int] NULL ,
[productid] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[summoney] [decimal](18, 9) NULL , --精度 4 改成6 翁
[sumamount] [decimal](18, 6) NULL ,--精度 4 改成6 翁
[price] [decimal](18,9) NULL --精度 4 改成6 翁
)
insert #temp(productid,storehouseid,sumamount,summoney,price)
select productid,@houseid,sum(amount) as amount,sum(summoney) as summoney ,
case when sum(amount)=0 then 0 else round(sum(1.00000000*summoney)/sum(amount),6) end Price from --单价取6位小数 翁
(select itemid,productid,amount,summoney from p_productAccountSumTab where itemid=@endid and houseid=@houseid union all --这里增加了 all 翁
select '1',productid,sum(amount) as amount,sum(1.00000000*money) as money from kc_billjoinmaintab a
join kc_billjoinlisttab b on a.billdocument_id=b.billdocument_id
where operatedate>=@stdt and operatedate<@eddte+1 and storehouseid=@houseid group by productid) a
group by productid
select round(cast(sum(63883.63) as dec(30, 8))/sum(23625), 8) --这是我用分组计算出的单价
select 63883.63/23625 --手动算出的单价格
select round(cast(sum(63883.63) as dec(30, 8))/sum(23625), 6) --这是我用分组计算出的单价
select round(63883.63/23625, 6) --手动算出的单价格
-- 结果如下
----------------------------------------
2.7040690000000000
(所影响的行数为 1 行)
---------------
2.70406900
(所影响的行数为 1 行)
select cast(sum(63883.63)/sum(23625) as dec(30,6))
楼主打算要那一个结果?
--ROUND
--返回数字表达式并四舍五入为指定的长度或精度。
--
--语法
--ROUND ( numeric_expression , length [ , function ] )
--
--参数
--numeric_expression
--
--精确数字或近似数字数据类型类别的表达式(bit 数据类型除外)。
--
--length
--
--是 numeric_expression 将要四舍五入的精度。length 必须是 tinyint、smallint 或int。当 length 为正数时,numeric_expression 四舍五入为 length 所指定的小数位数。当 length 为负数时,numeric_expression 则按 length 所指定的在小数点的左边四舍五入。
--
--function
--
--是 要执行的操作类型。function 必须是 tinyint、smallint 或 int。如果省略 function 或 function 的值为 0(默认),numeric_expression 将四舍五入。当指定 0 以外的值时,将截断 numeric_expression。
--
--返回类型
--返回与 numeric_expression 相同的类型。
--
--注释
--ROUND 始终返回一个值。如果 length 是负数且大于小数点前的数字个数,ROUND 将返回 0。
--
--示例 结果
--ROUND(748.58, -4) 0
--
--当 length 是负数时,无论什么数据类型,ROUND 都将返回一个四舍五入的 numeric_expression。
--
--示例 结果
--ROUND(748.58, -1) 750.00
--ROUND(748.58, -2) 700.00
--ROUND(748.58, -3) 1000.00
--
--示例
--A. 使用 ROUND 和估计值
--下例显示两个表达式,说明使用 ROUND 函数且最后一个数字始终是估计值。
--
Select ROUND(123.9994, 3), ROUND(123.9995, 3)
GO
--下面是结果集:
--
--———– ———–
--123.9990 124.0000
--
--B. 使用 ROUND 和四舍五入的近似值
--下例显示四舍五入和近似值。
--
--语句 结果
Select ROUND(123.4545, 2)
--123.4500
Select ROUND(123.45, -2)
--100.00
--
--C. 使用 ROUND 截断
--下例使用两个 Select 语句说明四舍五入和截断之间的区别。第一个语句四舍五入结果。第二个语句截断结果。
--
--语句 结果
Select ROUND(150.75, 0)
--151.00
Select ROUND(150.75, 0, 1)
--150.00
select cast(cast(sum(63883.63) as dec(30,8))/cast(sum(23625) as dec(30,8)) as dec(30,6))
declare @n1 sql_variant, @n2 sql_variant
select @n1=sum(63883.63), @n2=sum(23625)
select type=SQL_Variant_property(@n1,'BaseType'),
[precision]=SQL_Variant_property(@n1,'Precision'),
[scale]=SQL_Variant_property(@n1,'scale')
/*
type Precision scale
-------------------------------
numeric 38 2
*/
select type=SQL_Variant_property(@n2,'BaseType'),
[precision]=SQL_Variant_property(@n2,'Precision'),
[scale]=SQL_Variant_property(@n2,'scale')
/*
type Precision scale
-------------------------------
int 10 0
*/
declare @n4 sql_variant
select @n4= sum(63883.63)/sum(23625)
select type=SQL_Variant_property(@n4,'BaseType'),
[precision]=SQL_Variant_property(@n4,'Precision'),
[scale]=SQL_Variant_property(@n4,'scale')
/*
type Precision scale
-------------------------------
numeric 38 6
*/
GO
----如果不用sum
declare @n1 sql_variant, @n2 sql_variant
select @n1=63883.63, @n2=23625
select type=SQL_Variant_property(@n1,'BaseType'),
[precision]=SQL_Variant_property(@n1,'Precision'),
[scale]=SQL_Variant_property(@n1,'scale')
/*
type Precision scale
-------------------------------
numeric 7 2
*/
select type=SQL_Variant_property(@n2,'BaseType'),
[precision]=SQL_Variant_property(@n2,'Precision'),
[scale]=SQL_Variant_property(@n2,'scale')
/*
type Precision scale
-------------------------------
int 10 0
*/
declare @n4 sql_variant
select @n4= 63883.63/23625
select type=SQL_Variant_property(@n4,'BaseType'),
[precision]=SQL_Variant_property(@n4,'Precision'),
[scale]=SQL_Variant_property(@n4,'scale')
/*
type Precision scale
-------------------------------
numeric 13 8
*/
select cast(cast(sum(63883.63) as dec(28,6))/sum(23625) as dec(28,6))
/*
---------------------------------------
2.704069
(1 行受影响)
*/
select cast(sum(63883.63)/sum(23625) as decimal(18,6))
select sum(63883.63)*1.000000/sum(23625)