访问 SQL 查询内部连接并与 null 求和
Access SQL query inner join & sum with null
表:项目、采购、报价行。
项目有字段 ItemID、ItemName、IsActive
采购有字段 ItemID、QtyPurchased
QuoteLines 有字段 ItemID、NegQty、IsSold
目标是 select 所有 ItemID,其中 IsActive=yes,然后对所有已购买的数量求和,仅减去 IsSold=yes 的 NeqQty
我试过:
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;
这导致 "your query does not include the specified expression IIF(NegQTY=NULL, SUM(QtyPurchased), SUM(NegQty+PurchasedQty))"
我还尝试了 tblItems 和 tblPurchases 之间的内部连接以获取 PurchasedQty,然后在 tblItems 和 tblQuoteLines 之间进行第二个内部连接以仅获取 SoldQty,然后在 PurchasedQty 和 SoldQty 之间进行左内部连接以获取 InventoryQty,但这为同时存在于 PurchasedQty 和 SoldQty 中的项目提供正确的值,但对于仅存在于 PurchasedQty 中的项目,它返回 NULL。
考虑使用保存的查询来计算购买的总数量和销售的总数量,然后在计算当前库存的查询中使用这些保存的查询。
购买的总单位数
创建名为 [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));
表:项目、采购、报价行。
项目有字段 ItemID、ItemName、IsActive
采购有字段 ItemID、QtyPurchased
QuoteLines 有字段 ItemID、NegQty、IsSold
目标是 select 所有 ItemID,其中 IsActive=yes,然后对所有已购买的数量求和,仅减去 IsSold=yes 的 NeqQty
我试过:
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;
这导致 "your query does not include the specified expression IIF(NegQTY=NULL, SUM(QtyPurchased), SUM(NegQty+PurchasedQty))"
我还尝试了 tblItems 和 tblPurchases 之间的内部连接以获取 PurchasedQty,然后在 tblItems 和 tblQuoteLines 之间进行第二个内部连接以仅获取 SoldQty,然后在 PurchasedQty 和 SoldQty 之间进行左内部连接以获取 InventoryQty,但这为同时存在于 PurchasedQty 和 SoldQty 中的项目提供正确的值,但对于仅存在于 PurchasedQty 中的项目,它返回 NULL。
考虑使用保存的查询来计算购买的总数量和销售的总数量,然后在计算当前库存的查询中使用这些保存的查询。
购买的总单位数
创建名为 [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));