1

実行速度がはるかに速いため、SQL ステートメントを使用して Crystal レポートを作成しようとしています。しかし、いくつかのリンクに問題があります。リンクの結果を後続のリンクの基準に使用する必要があります。

わかりました、ここに私のステートメントがどのように見えるかのサンプルがあります:

(**印の行が該当行です)

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

これは、別のサブクエリ (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 に答える
1

問題は、実行できないサブクエリ内でエイリアスの 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

クエリをさらに見てみると、実際には 2 つのサブクエリを取り除くことができます。

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 に答える