“遇到了因零误差造成的误差”,T0和T3文档中有空值,并且不能执行空值

问题描述 投票:0回答:1

我在SQL查询中得到除以零的错误,有人知道为什么会这样吗?


           SELECT 
           T4.CardCode as 'Customer Code', 
           T4.CardName as 'Customer Name', 
           T0.DocEntry as 'Invoice No',
            ISNULL(T0.DocTotal, 0 ) as 'Invoice Amount', 
            ISNULL(T3.DocTotal, 0 ) as 'Credit Note', 
            ISNULL((T3.DocTotal/T0.DocTotal), 0)*100 as 'Credit Note%', 
            ISNULL((T0.DocTotal-T3.DocTotal), 0) as 'Recovered'
            ,CASE WHEN ISNULL(T3.DocTotal, 0 ) = 0 AND  ISNULL((T0.DocTotal), 0) = 0 THEN 0
            ELSE
             ((ISNULL((T0.DocTotal -T3.DocTotal), 0))/SUM((ISNULL(T0.DocTotal, 0))-(ISNULL(T3.DocTotal, 0)))*100) end  as 'Sales%'

             FROM OINV T0
             INNER JOIN INV1 T1 ON T0.DocEntry = T1.DocEntry
             LEFT OUTER JOIN RIN1 T2 ON T1.DocEntry = T2.BaseEntry
             LEFT OUTER JOIN ORIN T3 ON T2.DocEntry = T3.DocEntry
             INNER JOIN OCRD T4 ON T0.CardCode = T4.CardCode

             Group by T4.CardCode, T4.CardName,  T0.DocEntry, T0.DocTotal, T3.DocTotal
             Order by T4.CardCode, T0.DocEntry 
sql null sap ssms isnull
1个回答
0
投票

我看到的唯一部分是:

. . . / SUM((ISNULL(T0.DocTotal, 0))-(ISNULL(T3.DocTotal, 0))) * 100

您的逻辑应该是:

100.0 * . . . / NULLIF(SUM(T0.DocTotal) - COALESCE(T3.DocTotal, 0), 0)

即,如果表达式为NULL,则分母被0替换。这样可以防止被零除。

我移动了100.0 *只是因为我不想找出分子是乘还是分母。

© www.soinside.com 2019 - 2024. All rights reserved.