表:项目,购买,QuoteLines。
项目包含字段ItemID,ItemName,IsActive
购买具有字段ItemID,QtyPurchased
QuoteLines有字段ItemID,NegQty,IsSold
目标是选择所有ItemID,其中IsActive = yes然后对所有QtyPurchased求和并仅减去NeqQty,其中IsSold =是
我试过了:
SELECT Items.ItemID, Items.ItemName, IIF(NegQTY=NULL, SUM(QtyPurchased), SUM(NegQty+PurchasedQty)) AS Inv_Qty
FROM Purchases, Items, QuoteLines
WHERE Purchases.ItemID=Items.ItemID AND Items.ItemID=QuoteLines.ItemID
GROUP BY Items.ItemID, Items.ItemName;
这会导致"您的查询不包含指定的表达式IIF(NegQTY = NULL,SUM(QtyPurchased),SUM(NegQty + PurchasedQty))"
我还尝试了tblItems和tblPurchases之间的内部联接以获取PurchasedQty然后获取tblItems和tblQuoteLines之间的第二个内部联接以仅获取SoldQty,然后使用PurchasedQty和SoldQty之间的左内联接以获得InventoryQty,但是这给了在PurchasedQty和SoldQty中存在的项目的正确值,但对于仅存在于PurchasedQty中的项目,它返回NULL。
答案 0 :(得分:0)
考虑使用已保存的查询来计算购买的总单位数和已售出的总单位数,然后在查询中使用这些已保存的查询来计算当前广告资源。
创建名为[TotalPurchasedByItemID]
的已保存查询
SELECT
Items.ItemID,
Sum(Purchases.QtyPurchased) AS SumOfQtyPurchased
FROM
Items
INNER JOIN
Purchases
ON Items.ItemID = Purchases.ItemID
WHERE (((Items.IsActive)=True))
GROUP BY Items.ItemID;
创建名为[TotalSoldByItemID]
的已保存查询
SELECT
Items.ItemID,
Sum(QuoteLines.NegQty) AS SumOfNegQty
FROM
Items
INNER JOIN
QuoteLines
ON Items.ItemID = QuoteLines.ItemID
WHERE (((Items.IsActive)=True) AND ((QuoteLines.IsSold)=True))
GROUP BY Items.ItemID;
SELECT
Items.ItemID,
Items.ItemName,
Nz([SumOfQtyPurchased],0)-Nz([SumOfNegQty],0) AS Inv_Qty
FROM
(
Items
LEFT JOIN
TotalPurchasedByItemID
ON Items.ItemID = TotalPurchasedByItemID.ItemID
)
LEFT JOIN
TotalSoldByItemID
ON Items.ItemID = TotalSoldByItemID.ItemID
WHERE (((Items.IsActive)=True));