[英]access 2016 query
I need some help with creating a query. 我在创建查询时需要一些帮助。 This is an inventory application where Product1 is moved from location A to location B and then to location C. Location B should then have zero inventory for Product1. 这是一个清单应用程序,其中将Product1从位置A移动到位置B,然后再移动到位置C。然后,位置B应为Product1拥有零库存。 I have a table that stores all the movements as follows: 我有一个表,存储所有的运动,如下所示:
Movements (
ProductName,
QtyMoved,
LocationFrom,
LocationTo
)
Two entries might be: 两个条目可能是:
ProductName | QtyMoved | LocationFrom | LocationTo
------------+----------+---------------------------
"Product1" | 500 | "LocationA" | "LocationB"
"Product1" | 500 | "LocationB" | "LocationC"
This query lists the above entries: 该查询列出了以上条目:
SELECT
ProductName, QtyMoved, LocationFrom, LocationTo
FROM
Movements
WHERE
LocationFrom = 'LocationB'
OR
LocationTo = 'locationB';
What I want is to calculate and list the total Qty in 'locationB' for 'Product1', (which would be zero, in this case). 我想要的是计算并列出“ Product1”在“ locationB”中的总数量(在这种情况下为零)。
I would like the datasheet view of the query to show 3 columns, ProductName
, QtyMoved
and 'LocationB'
. 我希望查询的数据表视图显示3列ProductName
, QtyMoved
和'LocationB'
。 I believe what I need is an expression like: 我相信我需要的是这样的表达:
(total qty of Product1 in locationFrom for locationB) minus (total qty of Product1 in locationTo for locationB).
Perhaps this would be easier if I was to create a report instead of a query. 如果我要创建报告而不是查询,这可能会更容易。 Any suggestions would be appreciated. 任何建议,将不胜感激。
I would make a UNION query to add the moves into a location and subtract the moves out of a location - sommething like 我会做一个UNION查询,将动作添加到某个位置,然后从某个位置减去该动作-有点像
Select ProductName, LocationTo as Location, QtyMoved as Balance from Movements
Union All Select ProductName, LocationFrom, -QtyMoved from Movements
After you save this query, you can use it as the source for a 2nd query to get the balances, something like 保存此查询后,您可以将其用作第二次查询的来源以获取余额,例如
Select ProductName, Location, Sum(Balance) as Balance from [Movements Summary]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.