我有一个表达式,显示所有订单进出前的总库存:
(Query3只是将股票清单与进入和离开的订单组合在一起,并且工作正常)
SELECT Query3.Products.ID, Query3.ProductName, Query3.StandardCost, Query3.OnHand,
Nz(Sum([OrderJoin.Quantity]),0) AS Outgoing, Nz(Sum([Query1.Quantity]),0) AS Incoming,
[OnHand]+[Outgoing]-[Incoming] AS OnHandAfter, [StandardCost]*[OnHandAfter] AS TotalCost
FROM Query3
GROUP BY Query3.Products.ID, Query3.ProductName, Query3.StandardCost, Query3.OnHand;
但我一直试图通过用户输入的日期过滤它。我的代码不正确,MS Access只是说在JOIN操作中有语法错误,但没有说明在哪里/什么。
SELECT Q1.Products.ID, Q1.ProductName, Q1.StandardCost, Q1.OnHand,
[Q1.OnHand]+[Q2.Outgoing]-[Q3.Incoming] AS OnHandAfter,
[StandardCost]*[OnHandAfter] AS TotalCost
FROM (Query3 Q1
LEFT JOIN
(SELECT Q2.Query3.Products.ID, Q2.Query3.ProductName, Q2.Query3.StandardCost,
Q2.Query3.OnHand, NZ(Sum([OrderJoin.Quantity]),0) AS Outgoing
FROM Query3 Q2
WHERE (((Query3.ShippedDate)>[Enter End Date] Or (Query3.ShippedDate) Is Null))
GROUP BY Q2.Query3.Products.ID, Q2.Query3.ProductName, Q2.Query3.StandardCost,
Q2.Query3.OnHand) ON Q1.Query3.Products.ID = Q2.Query3.Products.ID)
LEFT JOIN
(SELECT Q3.Query3.Products.ID, Q3.Query3.ProductName, Q3.Query3.StandardCost,
Q3.Query3.OnHand, NZ(Sum([Query1.Quantity]),0) AS Incoming
FROM Query3 Q3
WHERE (((Query3.DateReceived)>[Enter End Date] Or (Query3.DateReceived) Is Null))
GROUP BY Q3,Query3.Products.ID, Q3.Query3.ProductName, Q3.Query3.StandardCost,
Q3.Query3.OnHand) ON Q1.Query3.Products.ID = Q3.Query3.Products.ID
GROUP BY Q1.Query3.Products.ID, Q1.Query3.ProductName, Q1.Query3.StandardCost, Q1.Query3.OnHand;
答案 0 :(得分:2)
您有多个问题:
<table/alias>.<field>
领域。例如:Q2.Query3.Products.Id
没有意义,或许Q2.ID
是什么意思?你要去哪个表和哪个领域?FROM table1 t1 LEFT JOIN (SELECT field FROM table) ON..
时,它应为FROM table1 t1 LEFT JOIN (SELECT field FROM table) AS <Alias> ON..
OnHandAfter
和TotalCost
未使用Sum,Avg,Max等进行汇总。您的格式应如下所示:
SELECT Q1.ID,
Q1.ProductName,
Q1.StandardCost,
Q1.OnHand,
SUM([Q1.OnHand] + [Q2_sub.Outgoing] - [Q3_sub.Incoming]) AS OnHandAfter,
SUM([StandardCost] * [OnHandAfter]) AS TotalCost
FROM
(
Query3 Q1
LEFT JOIN
(
SELECT Q2.ID,
Q2.ProductName,
Q2.StandardCost,
Q2.OnHand,
NZ(Sum([OrderJoin.Quantity]), 0) AS Outgoing
FROM Query3 Q2
WHERE (
(
(Query3.ShippedDate) > [Enter End Date]
OR (Query3.ShippedDate) IS NULL
)
)
GROUP BY Q2.ID,
Q2.ProductName,
Q2.StandardCost,
Q2.OnHand
) Q2_Sub ON Q1.ID = Q2_Sub.ID
)
LEFT JOIN (
SELECT Q3.ID,
Q3.ProductName,
Q3.StandardCost,
Q3.OnHand,
NZ(Sum([Query1.Quantity]), 0) AS Incoming
FROM Query3 Q3
WHERE (
(
(Query3.DateReceived) > [Enter End Date]
OR (Query3.DateReceived) IS NULL
)
)
GROUP BY Q3,
Query3.ID,
Q3.ProductName,
Q3.StandardCost,
Q3.OnHand
) Q3_Sub ON Q1.ID = Q3_Sub.ID
GROUP BY Q1.ID,
Q1.ProductName,
Q1.StandardCost,
Q1.OnHand;
答案 1 :(得分:0)
这是猜测,但看起来你没有别名你的子查询。你在子查询中有Q2和Q3但不在外面。别名是上下文的,因此在子查询之外它们并不意味着什么。如果你将它们移到外面,我认为它会有所帮助。
我认为你也有一些别名混淆。来自Karate Kid的Miyagi先生说“做或不做”,但你似乎在使用它们然后不使用它们。
此外,Access对括号有点疯狂。有时我想尽可能多地删除,看看实际发生了什么。
为了帮助说明这一点,我将子查询上的别名重命名为X而不是Q.
SELECT
Q1.Products.ID, Q1.ProductName, Q1.StandardCost, Q1.OnHand,
[Q1.OnHand]+[Q2.Outgoing]-[Q3.Incoming] AS OnHandAfter,
[StandardCost]*[OnHandAfter] AS TotalCost
FROM
Query3 Q1
LEFT JOIN
(SELECT
X2.Products.ID, X2.ProductName, X2.StandardCost,
X2.OnHand, NZ(Sum(X2.[Quantity]),0) AS Outgoing
FROM Query3 X2
WHERE
X2.ShippedDate > [Enter End Date] Or X2.ShippedDate Is Null
GROUP BY
X2.Products.ID, X2.ProductName, X2.StandardCost, X2.OnHand
) Q2 ON Q1.Products.ID = Q2.Products.ID
LEFT JOIN
(SELECT
X3.Products.ID, X3.ProductName, X3.StandardCost,
X3.OnHand, NZ(Sum([X3.Quantity]),0) AS Incoming
FROM Query3 X3
WHERE
Query3.DateReceived > [Enter End Date] Or X3.DateReceived Is Null
GROUP BY
X3.Products.ID, X3.Query3.ProductName, X3.StandardCost, X3.OnHand
) Q3 ON Q1.Products.ID = Q3.Products.ID
GROUP BY
Q1.Products.ID, Q1.ProductName, Q1.StandardCost, Q1.OnHand;
另外,在你的第三行到最后一行你有一个拼写错误:
GROUP BY Q3,Query3
这个逗号可能不是你的意思。