用ISNULL包装和计算

时间:2015-01-06 16:34:28

标签: sql sql-server sql-server-2008

我试图用零

替换此代码中的空值
SUM(ITR_TransQty * - 1)

我尝试将其包装在ISNULL中,但我仍然返回空值

ISNULL(SUM(ITR_TransQty * - 1),0)

我的代码中是否遗漏了某些内容,或者这是否与sum表达式中的计算无关?

谢谢!

使用完整查询进行更新

SELECT pod.POD_ItemID, pod.POD_PurchOrderID, pod.POD_RequiredDate,    pod.POD_RequiredQty, (pod.POD_RequiredQty * item.IMA_AcctValAmt) as OrderValue, 
((item.IMA_OrderPointQty * 2) + item.IMA_OnHandQty) AS MaxOHLogic, (item.IMA_AcctValAmt * item.IMA_OnHandQty) as OnHandValue, 
item.IMA_BuyerPlanner, aa.Last90, bb.Last180  
from 
POD INNER JOIN Item on IMA_ItemID = pod.POD_ItemID
left outer join
(SELECT     ITR_ItemID AS Item, ISNULL(SUM(ITR_TransQty * - 1),0) AS Last90
FROM          dbo.ITR (NOLOCK) 
WHERE      (ITR_TransDate BETWEEN GETDATE() - 92 AND GETDATE() - 2) AND 
(ITR_TransType LIKE N'Issue' OR
ITR_TransType LIKE N'Shipment')GROUP BY ITR_ItemID) AS aa ON dbo.Item.IMA_ItemID = aa.Item 
LEFT OUTER JOIN
(SELECT     ITR_ItemID AS Item, SUM(ITR_TransQty) * - 1 AS Last180
FROM          dbo.ITR (NOLOCK) 
WHERE      (ITR_TransDate BETWEEN GETDATE() - 183 AND GETDATE() - 93) AND (ITR_TransType LIKE N'Issue' OR
ITR_TransType LIKE N'Shipment')
GROUP BY ITR_ItemID) AS bb ON dbo.Item.IMA_ItemID = bb.Item
WHERE ((Item.IMA_OrderPointQty * 2) <= item.IMA_OnHandQty) AND (pod.POD_RequiredDate >= GETDATE()-30) AND (pod.POD_RequiredDate <=
GETDATE()+90) AND (pod.POD_CompleteFlag <> 1)
order by OrderValue desc

将代码更改为SUM(ISNULL(ITR_TransQty,0)* - 1)并且我仍然返回NULL值。我刚检查了这个专栏,发现它是FLOAT而不是INT,我想我需要先将它转换成它吗?

2 个答案:

答案 0 :(得分:2)

使用时没有错:

ISNULL(SUM(ITR_TransQty * - 1),0)
聚合中会忽略

NULL个值,因此仅当ISNULL()的所有值均为ITR_TransQty时才会使用NULL

您返回NULL值的原因是您在子查询中选择聚合,然后使用LEFT JOIN来访问。

您需要在ISNULL()列表中使用COALESCE()SELECT删除NULL

    SELECT pod.POD_ItemID, pod.POD_PurchOrderID, pod.POD_RequiredDate,    pod.POD_RequiredQty, (pod.POD_RequiredQty * item.IMA_AcctValAmt) as OrderValue, 
    ((item.IMA_OrderPointQty * 2) + item.IMA_OnHandQty) AS MaxOHLogic, (item.IMA_AcctValAmt * item.IMA_OnHandQty) as OnHandValue, 
    item.IMA_BuyerPlanner
   , ISNULL(aa.Last90,0) AS Last90  
   , bb.Last180  
    FROM ...

此更改会使您的聚合周围的ISNULL()多余,因此您可以删除它。

答案 1 :(得分:2)

您需要使用ISNULL将aa.Last90包装在外部选择中,因为您使用的是左外连接。

SELECT pod.POD_ItemID, pod.POD_PurchOrderID, pod.POD_RequiredDate,    pod.POD_RequiredQty, (pod.POD_RequiredQty * item.IMA_AcctValAmt) as OrderValue, 
((item.IMA_OrderPointQty * 2) + item.IMA_OnHandQty) AS MaxOHLogic, (item.IMA_AcctValAmt * item.IMA_OnHandQty) as OnHandValue, 
item.IMA_BuyerPlanner, ISNULL(aa.Last90, 0) AS [Last90], bb.Last180  
from 
POD INNER JOIN Item on IMA_ItemID = pod.POD_ItemID
left outer join
(SELECT     ITR_ItemID AS Item, SUM(ITR_TransQty * - 1) AS Last90
FROM          dbo.ITR (NOLOCK) 
WHERE      (ITR_TransDate BETWEEN GETDATE() - 92 AND GETDATE() - 2) AND 
(ITR_TransType LIKE N'Issue' OR
ITR_TransType LIKE N'Shipment')GROUP BY ITR_ItemID) AS aa ON dbo.Item.IMA_ItemID = aa.Item 
LEFT OUTER JOIN
(SELECT     ITR_ItemID AS Item, SUM(ITR_TransQty) * - 1 AS Last180
FROM          dbo.ITR (NOLOCK) 
WHERE      (ITR_TransDate BETWEEN GETDATE() - 183 AND GETDATE() - 93) AND (ITR_TransType LIKE N'Issue' OR
ITR_TransType LIKE N'Shipment')
GROUP BY ITR_ItemID) AS bb ON dbo.Item.IMA_ItemID = bb.Item
WHERE ((Item.IMA_OrderPointQty * 2) <= item.IMA_OnHandQty) AND (pod.POD_RequiredDate >= GETDATE()-30) AND (pod.POD_RequiredDate <=
GETDATE()+90) AND (pod.POD_CompleteFlag <> 1)
order by OrderValue desc