救命啊! 我有一张如下表所示的表。
SELECT *
FROM tblshipline
WHERE MethodShipped = 'FEDEX'
然后我就明白了
Qtyshipped
2
3
15
3
10
9
10
现在的问题是计算 qtyshipped
的平均值并将其四舍五入到小数点后两位。
我的代码是
SELECT CONVERT(decimal(8,6), ROUND(AVG(Qtyshipped),2))
FROM TblShipLine
WHERE MethodShipped= 'FEDEX'
答案应该是7.430000
,
但它总是返回7.000000
。我很感激任何建议,谢谢!
最佳答案
在这里,我们获取您的 INT 值并添加 0.0,这是对 float 的快速转换。然后我们应用round()和convert()
Declare @YourTable table (Qtyshipped int)
Insert into @YourTable values
(2),
(3),
(15),
(3),
(10),
(9),
(10)
Select convert(decimal(8,6),round(Avg(Qtyshipped+0.0),2))
From @YourTable
返回
7.430000
关于sql - 如何在 SQL SERVER 中将 AVG() 函数转换为十进制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42741245/