在SQL Server中仅显示2个小数位数

时间:2017-04-13 16:15:21

标签: tsql sql-server-2014

我有一些存储为INT值的列,我正在做一些加法和除法,我想通过将小数点后的数字限制为2来显示结果。我尝试了不同的组合DECIMAL / NUMERIC / ROUND但我无法得到解决方案。

有人可以提供有关如何获得所需解决方案的任何建议吗?

查询:

SELECT cc.code AS [CountyID], RTRIM(LTRIM(cc.[description])) AS [CountyName],
SUM(ISNULL(ps.push_count,0)) AS [CountyPushCounts],
SUM(ISNULL(ps.push_unique_count,0)) AS [UniquePushCount],
SUM(ISNULL(ps.error_count,0)) AS [PushErrorCount],
SUM(ISNULL(ps.warning_count,0)) AS [PushWarningCount],

(CAST(SUM(ISNULL(ps.push_unique_count,0)) AS DECIMAL(15,2)) / CAST(SUM(ISNULL(ps.push_count,0)) AS DECIMAL(15,2)) * 100.0) AS [Unique Push Per Day] ,

((CAST(SUM(ISNULL(ps.error_count,0)) AS DECIMAL(15,2)) + CAST(SUM(ISNULL(ps.warning_count,0)) AS DECIMAL(15,2))) / CAST(SUM(ISNULL(ps.push_count,0)) AS DECIMAL(15,2)) * 100.0) AS [Data Error Rate]

FROM dbo.push_stats AS [ps]
INNER JOIN CCIS.dbo.county_codes AS [cc] ON ps.county_code = cc.code
WHERE DATEPART(YEAR,ps.ldstat_date) = 2017
AND DATEPART(MONTH,ps.ldstat_date) = 3
GROUP BY cc.code, cc.[description]
ORDER BY cc.[description];

我的数据集如下:

CountyID  CountyName  PushCounts  UniquePushCount  PushErrorCount  PushWarningCount  [Unique Push Per Day]  [Data Error Rate]
1         ALACHUA     210422      77046            0               39                36.61499273            0.018534184
2         BAKER       8099        5306             0               71                65.51426102            0.876651438
3         BAY         3178214     434893           117             2793              13.68356568            0.091560857
4         BRADFORD    17654       12119            0               131               68.64733205            0.742041464

2 个答案:

答案 0 :(得分:1)

你必须投出最终结果,而不仅仅是个人分子和分母

DECLARE @Numerator INTEGER
DECLARE @Denominator INTEGER

SET @Numerator = 10
SET @Denominator = 3;
-- This will produce 3.33333333
SELECT CAST(@Numerator AS DECIMAL(5,2))/CAST(@Denominator AS DECIMAL(5,2)) 
-- This will give you 3.33
SELECT CAST(
     CAST(@Numerator AS DECIMAL(5,2))
          /CAST(@Denominator AS DECIMAL(5,2)) 
       AS DECIMAL(5,2))

答案 1 :(得分:1)

我认为这样做会:

SELECT cc.code AS [CountyID], RTRIM(LTRIM(cc.[description])) AS [CountyName],
    SUM(ISNULL(ps.push_count,0)) AS [CountyPushCounts],
    SUM(ISNULL(ps.push_unique_count,0)) AS [UniquePushCount],
    SUM(ISNULL(ps.error_count,0)) AS [PushErrorCount],
    SUM(ISNULL(ps.warning_count,0)) AS [PushWarningCount],

    CAST((SUM(ISNULL(ps.push_unique_count,0)) * 100.00) / SUM(ISNULL(ps.push_count,0)) AS DECIMAL(15,2)) AS [Unique Push Per Day] ,   
    CAST((SUM(ISNULL(ps.error_count,0)) + SUM(ISNULL(ps.warning_count,0)) * 100.00) / SUM(ISNULL(ps.push_count,0)) AS DECIMAL(15,2)) AS [Data Error Rate]

FROM dbo.push_stats AS [ps]
INNER JOIN CCIS.dbo.county_codes AS [cc] ON ps.county_code = cc.code
WHERE DATEPART(YEAR,ps.ldstat_date) = 2017
    AND DATEPART(MONTH,ps.ldstat_date) = 3
GROUP BY cc.code, cc.[description]
ORDER BY cc.[description];

这里有两个要点:

  1. 使用除法运算,在除法发生之前,至少将一个项赋予某种浮点类型,这样它就不会进行整数除法并截断结果的小数部分。只要任一术语是某种浮点类型,你就可以了,只需先移动* 100.00即可完成此任务。
  2. 您希望允许内部计算大于2位小数,并且仅在最后设置输出格式。在表达式中过早地舍入或转换为有限类型可以在最终结果中夸大地以小的方式改变中间值。这意味着您只需要在整个集合中进行一次大CAST()次操作。