TSQL除以零误差

时间:2014-10-18 17:22:25

标签: sql-server sql-server-2008 tsql stored-procedures

我有一个正在进行数学运算的查询,并且有时候数字可能为零,除以它会导致错误。

我在这个网站上发现了一些关于如何修复它的内容,但现在这个数字根本没有变化。

Example Data: 
los.shortTermLosses = 1
A.shortTerm = 15
Giving the equation of 1/15*12 = 0.8

COALESCE(los.shortTermLosses / NULLIF(A.shortTerm,0),0.00)* 12 AS shortTermAttrition

我必须采取措施防止零差错,但不确定如何使其正常工作。当前结果始终为0.00

更新

对于那些想要查看整个查询的人..

SELECT A.QID,
          (SELECT    TOP 1 E.[FirstName],
                           E.[LastName],
                           E.[NTID],
                           E.[TitleDesc],
                           A.[countOfDirects],
                           A.[longTerm],
                           A.[shortTerm],
                           COALESCE(los.totalLosses,0) totalLosses,
                           COALESCE(los.longTermLosses, 0) longTermLosses,
                           COALESCE(los.shortTermLosses,0) shortTermLosses,
                           COALESCE(los.shortTermLosses / NULLIF(A.shortTerm,0),0.00)* 12 AS shortTermAttrition,
                           COALESCE(los.longTermLosses / NULLIF(A.longTerm,0),0.00)* 12 AS longTermAttrition,
                           COALESCE(los.totalLosses / NULLIF(A.countOfDirects,0),0.00)* 12 AS totalAttrition
            FROM     employeeTable_historical AS E
            OUTER APPLY (SELECT         COUNT(b.leaver) as [totalLosses],
                                        sum(case when b.term = 'LTA' then 1 else 0 end) as [longTermLosses],
                                        sum(case when b.term = 'STA' then 1 else 0 end) as [shortTermLosses]
                                FROM dbo.attritionData AS B
                                WHERE E.QID = B.supervisor
                                AND MONTH(B.leaveDate) = @month
                                AND YEAR(B.leaveDate) = @year
                                GROUP BY b.supervisor
            )los
            WHERE    E.qid = A.[QID]
                     AND CONVERT (DATE, dateadd(mm, (@year - 1900) * 12 + @month - 1 , @day - 1)) >= CONVERT (DATE, E.[Meta_LogDate])
            ORDER BY meta_logDate DESC
            FOR      XML PATH (''), TYPE, ELEMENTS)
    FROM   (SELECT QID,
                   [timestamp],
                   [countOfDirects],
                   [longTerm],
                   [shortTerm]
            FROM   (SELECT QID,
                           [timestamp],
                           [countOfDirects],
                           [shortTerm],
                           [longTerm],
                           ROW_NUMBER() OVER (PARTITION BY QID ORDER BY [Timestamp]) AS Row
                    FROM   [red].[dbo].[attritionCounts]
                    WHERE  [mgrQID] = @director
                           AND YEAR(CAST ([timestamp] AS DATE)) = @year
                           AND MONTH(CAST ([timestamp] AS DATE)) = @month) AS Tmp1
            WHERE  Row = 1) AS A
    FOR    XML PATH ('DirectReport'), TYPE, ELEMENTS, ROOT ('Root');

1 个答案:

答案 0 :(得分:1)

你的错误不是来自NULLIF,你只是划分一个整数,例如1/15 = 0
,只需将你的任期改为:
COALESCE(CAST(los.shortTermLosses as float) / NULLIF(A.shortTerm,0),0.00)* 12 AS shortTermAttrition

您最近的转换XML导出可能会将结果转换为资金

Declare  @shortTermLosses int = 1
Declare  @shortTerm int = 15

select 
CAST(
COALESCE(CAST(@shortTermLosses AS float) / Cast(NULLIF(@shortTerm,0) AS float),0.00)* 12 
as Money)
AS shortTermAttrition
FOR      XML PATH (''), TYPE, ELEMENTS