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

前端之家收集整理的这篇文章主要介绍了sql – 为什么将AVG(intger_column)转换为DECIMAL至少返回六位小数?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
考虑这个查询
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,6))) AS precision_six,5))) AS precision_five,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始终返回“十进制(38,s)除以十进制(10,0)”数据类型( see here)

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

猜你在找的MsSQL相关文章