1

我正在尝试使用 sql 语句编写水晶报告,因为它运行得更快。但是我在某些链接上遇到了麻烦。我需要在后续链接中使用链接的结果作为标准。

好的,这是我的陈述的示例:

(标有**的行是有问题的行)

SELECT
    Part.PartNum,
    Cust.CustNum,
    Cust.CustID,
    YTD.Qty
FROM
    (
    SELECT
        Pub.Part.PartNum,
        Pub.Part.UserChar1 AS CustID
    FROM
        Pub.Part
    ) AS Part
        LEFT OUTER JOIN (
            SELECT
                Pub.Customer.CustID,
                Pub.Customer.CustNum,
                Pub.Customer.Name
            FROM
                Pub.Customer
            WHERE
                Pub.Customer.CustID = '1038'
        ) AS Cust 
            ON Part.CustID = Cust.CustID
        LEFT OUTER JOIN (
            SELECT
                Pub.OrderDtl.PartNum,
                Sum(Pub.OrderDtl.OrderQty) AS Qty
            FROM
                Pub.OrderHed JOIN Pub.OrderDtl ON
                    Pub.OrderHed.OrderNum = Pub.OrderDtl.OrderNum
            WHERE
                **Pub.OrderHed.CustNum = Cust.CustNum AND**
                **Pub.OrderDtl.PartNum = Part.PartNum AND**
                YEAR(Pub.OrderHed.OrderDate)=YEAR(CURDATE())
            GROUP BY 
                Pub.OrderDtl.PartNum
        ) AS YTD ON Part.PartNum = YTD.PartNum

现在,我收到一条错误消息:

Part.PartNum cannot be found or is not specified for the query.

Cust.CustNum 出现同样的错误。你能帮我弄清楚我做错了什么吗?谢谢!

4

2 回答 2

1

问题是您在无法执行的子查询中使用了其中一个别名。你将不得不做类似的事情:

SELECT Part.PartNum,
  Cust.CustNum,
  Cust.CustID,
  YTD.Qty
FROM
(
  SELECT Pub.Part.PartNum,
    Pub.Part.UserChar1 AS CustID
  FROM Pub.Part
) AS Part
LEFT OUTER JOIN
(
  SELECT Pub.Customer.CustID,
    Pub.Customer.CustNum,
    Pub.Customer.Name
  FROM Pub.Customer
  WHERE Pub.Customer.CustID = '1038'
) AS Cust
  ON Part.CustID = Cust.CustID
LEFT OUTER JOIN
(
  SELECT Pub.OrderDtl.PartNum,
    Sum(Pub.OrderDtl.OrderQty) AS Qty,
    Pub.OrderHed.CustNum
  FROM Pub.OrderHed 
  JOIN Pub.OrderDtl 
    ON Pub.OrderHed.OrderNum = Pub.OrderDtl.OrderNum
  WHERE YEAR(Pub.OrderHed.OrderDate)=YEAR(CURDATE())
  GROUP BY Pub.OrderDtl.PartNum, Pub.OrderHed.CustNum
) AS YTD
  ON Part.PartNum = YTD.PartNum
  AND Cust.CustNum = YTD.CustNum

更多地查看您的查询,您实际上可以摆脱两个子查询:

SELECT Part.PartNum,
  Cust.CustNum,
  Cust.CustID,
  YTD.Qty
FROM Pub.Part Part
LEFT OUTER JOIN Pub.Customer Cust
  ON Part.CustID = Cust.CustID
  AND Cust.CustID = '1038'
LEFT OUTER JOIN
(
  SELECT d.PartNum,
    Sum(d.OrderQty) AS Qty,
    h.CustNum
  FROM Pub.OrderHed h
  JOIN Pub.OrderDtl d
    ON h.OrderNum = d.OrderNum
  WHERE YEAR(h.OrderDate)=YEAR(CURDATE())
  GROUP BY d.PartNum
) AS YTD
  ON Part.PartNum = YTD.PartNum
  AND Cust.CustNum = YTD.CustNum
于 2012-10-05T19:28:59.800 回答
1

这是因为您无法在另一个子查询 (YTD) 中访问父子查询 (cust, part)

但是,在您的情况下,解决方案很简单,而是在 ON 子句中过滤:

SELECT
    Part.PartNum,
    Cust.CustNum,
    Cust.CustID,
    YTD.Qty
FROM
    (
    SELECT
        Pub.Part.PartNum,
        Pub.Part.UserChar1 AS CustID
    FROM
        Pub.Part
    ) AS Part
        LEFT OUTER JOIN (
            SELECT
                Pub.Customer.CustID,
                Pub.Customer.CustNum,
                Pub.Customer.Name
            FROM
                Pub.Customer
            WHERE
                Pub.Customer.CustID = '1038'
        ) AS Cust 
            ON Part.CustID = Cust.CustID
        LEFT OUTER JOIN (
            SELECT
                Pub.OrderDtl.PartNum,
                Sum(Pub.OrderDtl.OrderQty) AS Qty,
                Pub.OrderHed.CustNum
            FROM
                Pub.OrderHed JOIN Pub.OrderDtl ON
                    Pub.OrderHed.OrderNum = Pub.OrderDtl.OrderNum
            WHERE
                YEAR(Pub.OrderHed.OrderDate)=YEAR(CURDATE())
            GROUP BY 
                Pub.OrderDtl.PartNum
        ) AS YTD ON Part.PartNum = YTD.PartNum AND Cust.CustNum = YTD.CustNum
于 2012-10-05T19:35:35.483 回答