0

我真的很难解决这个问题。我已经尝试了 FROM 部分的所有可能组合,但我似乎无法得到它。我试过逗号,混合内连接和左连接然后这个。

[ give some date pls]是参数提示(仅适用于 MS Access 2007),因此请忽略它。

它说我的 FROM 部分有问题。

SELECT
    TJI.Serial AS ID,
    FIRST(TJI.Product) AS Product,
    ROUND((((SUM(TJI.JobOrder) * FIRST(TJI.ProductPackQTY)) - IIF(ISNULL(SUM(TJI.TheAmount)), 0, SUM(TJI.TheAmount))) / FIRST(TJI.STDYield)) / FIRST(TJI.MinBatch), 0) * FIRST(TJI.MinBatch) AS Batch,
    IIF(Batch <= 0, FIRST(MinBatch), Batch) AS FinalBatch

FROM TBL_JO AS TJ INNER JOIN (SELECT * FROM TBL_JOITEMS LEFT JOIN RET_STOCKS ON (Serial = TheItemSerial AND TJ.Date = TheDate) AS TJI ON TJ.Serial = TJI.JO_Serial

WHERE (TJ.Date = [give some date pls])
GROUP BY TJI.Serial
4

2 回答 2

1

经过一段时间的试验,我终于找到了我一直在寻找的答案。感谢您给 Remou 的提示和技巧。这肯定是一个很大的帮助。

我已将 TBL_JO 与 RET_STOCKS 交换,而不是使用 INNER JOIN,而是使用另一种方法通过 WHERE 定义关系。

SELECT JOSerial, JODate, PSerial, Product, Category, JobOrder, Yield, Pack, MinBatch, IIF(ISNULL(TheAmount),0,TheAmount) AS ReturnStocks
FROM
    (SELECT TJ.Serial AS JOSerial, TJ.Date AS JODate, TJI.Serial AS PSerial, TJI.Product AS Product, TJI.Category AS Category, TJI.JobOrder AS JobOrder, TJI.STDYield AS Yield, TJI.ProductPackQTY AS Pack, TJI.MinBatch AS MinBatch 
        FROM TBL_JO AS TJ, TBL_JOITEMS AS TJI
        WHERE TJ.Serial=TJI.JO_Serial)
AS TJX
LEFT JOIN
    RET_STOCKS AS RS
ON
    (TJX.JODate=RS.TheDate)
    AND
    (TJX.PSerial=RS.TheItemSerial)
于 2012-06-17T16:25:35.237 回答
1

我不得不猜测您希望如何在派生表上进行联接,但请尝试以下几行:

SELECT
TJI.Serial AS ID,
FIRST(TJI.Product) AS Product,
ROUND((((SUM(TJI.JobOrder) * 
    FIRST(TJI.ProductPackQTY)) - 
    IIF(ISNULL(SUM(TJI.TheAmount)), 0, SUM(TJI.TheAmount))) / 
    FIRST(TJI.STDYield)) / 
    FIRST(TJI.MinBatch), 0) * FIRST(TJI.MinBatch) AS Batch,

IIF(Batch <= 0, FIRST(MinBatch), Batch) AS FinalBatch

FROM TBL_JO AS TJ
INNER JOIN (SELECT * FROM TBL_JOITEMS j
            LEFT JOIN RET_STOCKS r
            ON j.Serial =r.TheItemSerial) AS TJI 

ON TJ.Serial = TJI.JO_Serial AND TJ.Date = TJI.TheDate
GROUP BY TJI.Serial

如果您遇到上述错误并且没有突出显示 SQL 的任何部分,请始终怀疑子查询或派生表。在这种情况下,您正在使用两个表,但您的联接没有引用所涉及的表。此外,您的派生表中有一个杂散括号 -- ( -- 使之变得毫无意义。

顺便说一句,通常认为使用 * 是个坏主意,最好命名相关字段(列)。

于 2012-06-17T09:04:03.167 回答