gpt4 book ai didi

sql - 为什么将 AVG(intger_column) 转换为 DECIMAL 返回至少六位小数?

转载 作者:行者123 更新时间:2023-12-02 18:24:47 24 4
gpt4 key购买 nike

考虑这个查询:

WITH Scores (score)
AS
(
SELECT CAST(score AS INTEGER)
FROM (
VALUES (0),
(10),
(10)
) AS Scores (score)
)
SELECT AVG(CAST(score AS DECIMAL(19, 8))) AS precision_eight,
AVG(CAST(score AS DECIMAL(19, 7))) AS precision_seven,
AVG(CAST(score AS DECIMAL(19, 6))) AS precision_six,
AVG(CAST(score AS DECIMAL(19, 5))) AS precision_five,
AVG(CAST(score AS DECIMAL(19, 4))) AS precision_four
FROM Scores;

结果:

precision_eight | precision_seven | precision_six | precision_five | precision_four 
6.66666666 | 6.6666666 | 6.666666 | 6.666666 | 6.666666

为什么我总是得到至少六位小数?这是有记录的行为吗?

(我正在运行 SQL Server 2008)

最佳答案

小数的 AVG 始终返回“decimal(38, s) 除以decimal(10, 0)”数据类型 ( see here )

您必须将 AVG 结果转换为所需的精度。

关于sql - 为什么将 AVG(intger_column) 转换为 DECIMAL 返回至少六位小数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3943265/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com