使用 ISNULL 包装总和和计算
Wrapping sum and calculation with ISNULL
我正在尝试用零替换此代码中的空值
SUM(ITR_TransQty * - 1)
我尝试用 ISNULL 包装它,但我仍然返回空值
ISNULL(SUM(ITR_TransQty * - 1),0)
我的代码中是否遗漏了某些内容,或者这是否无法在总和表达式中计算 b/c?
谢谢!
更新完整查询
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,我想我需要先转换它吧?
使用没有问题:
ISNULL(SUM(ITR_TransQty * - 1),0)
NULL
值在聚合中被忽略,因此仅当 ITR_TransQty
的所有值都是 NULL
.
时才使用 ISNULL()
您返回 NULL
值的原因是您在子查询中选择聚合,然后使用 LEFT JOIN
到达。
您需要在 SELECT
列表中使用 ISNULL()
或 COALESCE()
来删除 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()
变得多余,因此您可以将其删除。
您需要用 ISNULL 代替外 select 中的 a.Last 90,因为您使用的是左外连接。
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
我正在尝试用零替换此代码中的空值
SUM(ITR_TransQty * - 1)
我尝试用 ISNULL 包装它,但我仍然返回空值
ISNULL(SUM(ITR_TransQty * - 1),0)
我的代码中是否遗漏了某些内容,或者这是否无法在总和表达式中计算 b/c?
谢谢!
更新完整查询
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,我想我需要先转换它吧?
使用没有问题:
ISNULL(SUM(ITR_TransQty * - 1),0)
NULL
值在聚合中被忽略,因此仅当 ITR_TransQty
的所有值都是 NULL
.
ISNULL()
您返回 NULL
值的原因是您在子查询中选择聚合,然后使用 LEFT JOIN
到达。
您需要在 SELECT
列表中使用 ISNULL()
或 COALESCE()
来删除 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()
变得多余,因此您可以将其删除。
您需要用 ISNULL 代替外 select 中的 a.Last 90,因为您使用的是左外连接。
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