[英]MS-Access: Calculate Stock From 3 Tables
I was reading this post from StackOverFlow
to adjust query to calculate stock from 3 tables.我正在阅读
StackOverFlow
的这篇文章,以调整查询以从 3 个表中计算库存。 I have 3 tables tblItems
, tblPurchase
and tblSales
.我有 3 个表
tblItems
、 tblPurchase
和tblSales
。 tblItems
contains items information like below. tblItems
包含如下所示的项目信息。
ItemCode Description ItemClass ItemCategory ReorderLevel Unit Stop
IT000001 Tonner 226A Tonner IT Accessories 10 Pc(s) No
IT000002 Keyborad (A4Tech, Slim Key) Keyboard IT Accessories 10 Pc(s) No
IT000003 Mouse (Wireless) Mouse IT Accessories 10 Pc(s) No
SI000001 A4 Size Paper Paper Office Stationary 10 Pc(s) No
SI000002 Pen (Black) Pen Office Stationary 10 Pc(s) No
SI000003 Pen (Blue) Pen Office Stationary 10 Pc(s) No
Screenshots of my tables我的桌子截图
tblItems: tblItems:
tblPurchase: tbl购买:
tblSales: tbl销售:
And my expected result like below我的预期结果如下
In query result items details will come from tblItems
.在查询结果项目详细信息将来自
tblItems
。 Sum purchase quantity per item and sum sales quantity per item then minus SalesQuantity
from PurchaseQuantity
to see stock.将每个项目的采购数量和每个项目的销售数量相加,然后从
PurchaseQuantity
中减去SalesQuantity
以查看库存。
Here is my query which gives me following warning.这是我的查询,它给了我以下警告。
SELECT DISTINCTROW tblItems.ItemCode, TblItems.Description, TblItems.ItemClass, TblItems.ItemCategory, TblPurchaseAgg.[pQuantity], TblSalesAggs.[sQuantity]
FROM (TblItems
LEFT JOIN (SELECT TblItems.ItemCode AS ItemCode, Sum(TblPurchase.Quantity) AS pQuantity FROM TblItems LEFT JOIN TblPurchase ON TblItems.ItemCode = TblPurchase.ItemCode GROUP BY TblItems.ItemCode)
AS TblPurchaseAgg ON TblItems.ItemCode = TblPurchaseAgg.ItemCode)
LEFT JOIN
(SELECT TblItems.ItemCode AS ItemCode, Sum(tblSales.Quantity) AS sQuantity FROM TblItems LEFT JOIN tblSales ON TblItems.ItemCode = tblSales.ItemCode GROUP BY TblItems.ItemCode) AS TblSalesAggs ON TblItems.ItemCode = TblSalesAggs.ItemCode
GROUP BY TblItems.ItemCode, TblItems.Description, TblItems.ItemClass, TblItems.ItemCategory;
Seeking help to resolve the warning and get my expected output.寻求帮助以解决警告并获得我预期的 output。 Any help is greatly appreciated.
任何帮助是极大的赞赏。 Thanks in advance.
提前致谢。
Could you pls use this -你能用这个吗-
SELECT
tblItems.ItemCode, TblItems.Description, TblItems.ItemClass, TblItems.ItemCategory, TblPurchaseAgg.[pQuantity], TblSalesAggs.[sQuantity]
FROM TblItems
LEFT JOIN (SELECT TblPurchase.ItemCode AS ItemCode, Sum(TblPurchase.Quantity) AS pQuantity FROM TblPurchase GROUP BY TblPurchase.ItemCode) TblPurchaseAgg ON TblItems.ItemCode = TblPurchaseAgg.ItemCode
LEFT JOIN (SELECT tblSales.ItemCode AS ItemCode, Sum(tblSales.Quantity) AS sQuantity FROM tblSales GROUP BY tblSales.ItemCode) TblSalesAggs ON TblItems.ItemCode = TblSalesAggs.ItemCode
GROUP BY TblItems.ItemCode, TblItems.Description, TblItems.ItemClass, TblItems.ItemCategory;
You don't need the outer/first group.您不需要外部/第一组。 Since your item table will
[probably]
will never have duplicate items the records returned will always be unique.由于您的项目表
[probably]
将永远不会有重复的项目,因此返回的记录将始终是唯一的。
the both left joins you are using is grouping by itemCode
so they also will only return one record per item.您正在使用的两个左连接都是按
itemCode
分组的,因此它们也只会返回每个项目的一条记录。 so grouping overall is not required.所以不需要整体分组。
this could work for you:这可能对你有用:
SELECT
tblItems.ItemCode,
TblItems.Description,
TblItems.ItemClass,
TblItems.ItemCategory,
TblPurchaseAgg.pQuantity,
TblSalesAggs.sQuantity,
(TblPurchaseAgg.pQuantity - TblSalesAggs.sQuantity) as stock,
TblItems.unit
FROM
(TblItems
LEFT JOIN
(
SELECT
TblPurchase.ItemCode AS ItemCode,
Sum(TblPurchase.Quantity) AS pQuantity
FROM
TblPurchase
GROUP BY TblPurchase.ItemCode
) AS TblPurchaseAgg
ON TblItems.ItemCode = TblPurchaseAgg.ItemCode)
LEFT JOIN
(
SELECT
tblSales.ItemCode AS ItemCode,
Sum(tblSales.Quantity) AS sQuantity
FROM
tblSales
GROUP BY tblSales.ItemCode
) AS TblSalesAggs
ON TblItems.ItemCode = TblSalesAggs.ItemCode
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.