我有一个表(Inventory_Line),其中LID是自动递增的,IID是指代库存日期的单个数字,PID是数字部分#。我们将此表用于库存。
LID IID NAME PID QTY
--- --- ------ --- ---
1 1 Part A 213 12
2 1 Part B 200 15
3 2 Part A 213 9
4 2 Part B 200 7
我们还有一个表Order_Line
OLID OID NAME PID QTY
---- --- ------ --- ---
1 217 Part A 213 12
2 217 Part B 200 15
3 218 Part A 213 9
4 218 Part B 200 7
我的目标是展示
((Previous Inventory Qty (Inventory_Line.IID=1)) AS PREV_INV +
(ORDERED Qty (Order_Line.OID = 217 AND 218)) AS ORDERED -
(Current Inventory Qty Inventory_Line.IID=2) AS CURRENT_INV) AS SOLD
WHERE PID = X
输出如下:
PID NAME PREV_INV ORDERED CURRENT_INV SOLD
--- ------ -------- ------- ----------- ----
213 Part A 12 21 9 24
我们正在使用MS Access,而且我对联接有一些经验,但我有点不知道如何解决这个问题。任何帮助将不胜感激。
答案 0 :(得分:1)
你是否正在考虑这些问题?
SELECT t.pid,
Nz([pi].[qty], 0) + Nz([o].[qty], 0) - Nz([s].[qty], 0) AS dat
FROM (((SELECT DISTINCT pid
FROM inventory_line) AS t
LEFT JOIN (SELECT *
FROM inventory_line
WHERE iid = 2) AS pi
ON t.pid = pi.pid)
LEFT JOIN (SELECT *
FROM inventory_line
WHERE iid = 3) AS s
ON t.pid = s.pid)
LEFT JOIN (SELECT *
FROM order_line
WHERE oid = 217
OR oid = 218) AS o
ON t.pid = o.pid
WHERE (( ( t.pid ) = [Enter:] ))
上面需要做一些工作,但是如果想要输出更多的输入,应该做些什么会更清楚。
重新评论
SELECT t.pid,
t.name,
pi.qty,
o.q,
s.qty,
Nz([pi].[qty]) + Nz([o].[q]) - Nz([s].[Qty]) AS sold
FROM (((SELECT DISTINCT pid,
[Name]
FROM inventory_line) AS t
LEFT JOIN (SELECT pid,
qty
FROM inventory_line
WHERE iid = 1) AS pi
ON t.pid = pi.pid)
LEFT JOIN (SELECT pid,
qty
FROM inventory_line
WHERE iid = 2) AS s
ON t.pid = s.pid)
LEFT JOIN (SELECT pid,
SUM(qty) AS q
FROM order_line
WHERE oid = 217
OR oid = 218
GROUP BY pid) AS o
ON t.pid = o.pid
WHERE (( ( t.pid ) = [Enter:] ))
答案 1 :(得分:0)
我无法确定SOLD列中的24来自哪里,但这是另一种解决方案
SELECT Order_Line.PID, Order_Line.NAME, DLookUp("[QTY]","[Order_Line]","[OID]=" & [OID]-1 & " AND PID=" & [PID]) AS PREV_INV, DSum("[QTY]","[Order_Line]","[PID]=" & [PID]) AS ORDERED, Order_Line.QTY AS CURRENT_INV FROM Order_Line;