在sql join where子句中使用字段

时间:2012-10-05 19:21:32

标签: sql crystal-reports openedge

我正在尝试使用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收到同样的错误。你能帮我搞清楚我做错了什么吗?谢谢!

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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