简体   繁体   English

在计算中包括零数字而不会被零误差除

[英]Include zero figures in calculation without getting divide by zero errors

I am attempting to calculate the average of a percentage that needs to take into account percentages that are zero. 我正在尝试计算一个百分比的平均值,该平均值需要考虑零百分比。

I am currently using the following formula to exclude those that will ultimately end up as zero, but this is skewing the figures. 我目前正在使用以下公式排除最终将最终为零的那些变量,但这使数字产生了偏差。

  SET     Average = (SELECT ROUND(AVG((CAST(Figure1 AS FLOAT)/CAST(Figure2 AS FLOAT))*100),2)
                              FROM       [Schema].[Table] m
                              WHERE      m.Figure1 > 0 and m.Figure2 > 0)

When I include the possible zero based percentages 当我包括可能的从零开始的百分比时

  SET     Average = (SELECT ROUND(AVG((CAST(Figure1 AS FLOAT)/CAST(Figure2 AS FLOAT))*100),2)
                              FROM       [Schema].[Table] m)

I obviously get the following error; 我显然收到以下错误;

Divide by zero error encountered. 除以零时遇到的错误。 The statement has been terminated. 该语句已终止。

How can I change this to include the zero based percentages without the error? 如何更改此值以包括从零开始的百分比而没有错误?

The problem would be dividing by zero - so only figure2 is relevant which is the divident. 这个问题将被零除进行-因此,只有figure2是相关的也就是红利。

Use a case statement to set a static value for that specific case (I chose 0 ): 使用case语句为该特定案例设置静态值(我选择了0 ):

SET Average = (SELECT ROUND(AVG(case when Figure2 = 0
                                     then 0
                                     else CAST(Figure1 AS FLOAT) / CAST(Figure2 AS FLOAT)
                                end * 100)
                            ,2)
               FROM [Schema].[Table] m)

One good way to prevent divide by zero is to use the NULLIF function, like this: 防止被零除的一种好方法是使用NULLIF函数,如下所示:

 SET     Average = (SELECT ROUND(AVG((CAST(Figure1 AS FLOAT)/NULLIF(CAST(Figure2 AS FLOAT), 0))*100),2)
                              FROM       [Schema].[Table] m)

Using the NULLIF method, the value NULL would be returned for the row(s) where Figure2 is 0. The AVG function ignores NULL values. 使用NULLIF方法,对于图2为0的行,将返回NULL值。AVG函数将忽略NULL值。 Because of this, please consider carefully whether this is the solution you want to use (because the results may be different than what you expect). 因此,请仔细考虑这是否是您要使用的解决方案(因为结果可能与您期望的有所不同)。

Well, the real problem is that division by zero produce undefined results, so they should not be part of any calculation. 好吧,真正的问题是被零除会产生不确定的结果,因此它们不应成为任何计算的一部分。

If you do decide to factor them in, you'll have to give them some nominal value. 如果您决定将它们考虑在内,则必须给它们一些标称值。 Let's say you want to use "zero" as that nominal value: you could do something like this: 假设您要使用“零”作为标称值:您可以执行以下操作:

SELECT @average = 
   AVG(   CASE
             WHEN COALESCE(CAST(figure2 AS Float), 0) = 0 THEN 0.0
             ELSE COALESCE(CAST figure1 AS Float), 0) / CAST(figure2 AS Float)
           END
       )
  FROM MyTable 
 WHERE Blah Blah Blah

While the answer of 而答案

SET Average = (SELECT ROUND(AVG(case when Figure2 = 0
                                 then 0
                                 else CAST(Figure1 AS FLOAT) / CAST(Figure2 AS FLOAT)
                            end * 100)
                        ,2)
           FROM [Schema].[Table] m)

is good if you want the records where Figure2 is zero to be average in as a zero, but I personally wouldn't want them at all and would use 如果您希望将Figure2为零的记录平均为零,那将是很好的选择,但我个人根本不希望使用它们,而是使用

SET Average = (SELECT ROUND(AVG(CAST(Figure1 AS FLOAT) / CAST(Figure2 AS FLOAT)
                             * 100)
                        ,2)
           FROM [Schema].[Table] m where Figure2 <> 0 and Figure2 is not null)

or perhaps this is a bonus points situation where you really have 4 points out of 0 points. 或这是一种奖励积分的情况,您实际上在0点中有4点。 Then I would do the following. 然后,我将执行以下操作。 Still taking into account if Figure2 really has all zeros in it's results. 仍要考虑到Figure2的结果中是否确实包含全零。

SET Average = ((SELECT CASE WHEN (SUM(CAST(Figure2 AS FLOAT))) <> 0 
           THEN ROUND(SUM(CAST(Figure1 AS FLOAT)) / SUM(CAST(Figure2 AS FLOAT))
           * 100
          ,2) ELSE 0 END
          FROM [Schema].[Table] m)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM