bugfix> sql-server > 投稿

ここにこの基本的なクエリがあり、2番目の列の結果を外部/関連クエリに関連付ける必要があります。

   SELECT 
        r1.OrderNo,
        COUNT(r1.PartNo) AS [Pcs Complete]
    FROM OrderRouting r1
    WHERE r1.Status = 'Current'
        AND r1.WorkCntr = 'Shipping'
    GROUP BY r1.OrderNo

出力:

だから私が試みているのは、外側のクエリの選択リストに2番目の列を入れることですので、次のようになります:

SELECT 
    CAST(r.OrderNo AS INT) AS OrderNo,
        (
        SELECT 
            r1.OrderNo,
            COUNT(r1.PartNo)
        FROM OrderRouting r1
        WHERE r1.Status = 'Current'
            AND r1.WorkCntr = 'Shipping'
        GROUP BY r1.OrderNo
        ) AS [Pcs Complete]
FROM OrderRouting r 
    JOIN OrderDet od ON r.JobNo = od.JobNo
    JOIN Orders o ON od.OrderNo = o.Orderno
WHERE r.Status = 'Current'
        AND r.WorkCntr IN ('Cutting', 'Framing', 'Assembly', 'Shipping')
GROUP BY CAST(r.OrderNo AS INT)

内側の選択では1列しか許可されないため、これは明らかに機能しませんが、r1.OrderNo列を削除すると、外側のクエリのすべての値に1つの集計が適用されますが、これは望ましくありません。それらをどのように相関させるのですか?私はこれを試しました:

SELECT 
    CAST(r.OrderNo AS INT) AS OrderNo,
        (
        SELECT 
            COUNT(r1.PartNo)
        FROM OrderRouting r1
        WHERE r1.Status = 'Current'
            AND r1.WorkCntr = 'Shipping'
        ) AS [Pcs Complete]
FROM OrderRouting r 
    JOIN OrderDet od ON r.JobNo = od.JobNo
    JOIN Orders o ON od.OrderNo = o.Orderno
WHERE r.Status = 'Current'
        AND r.WorkCntr IN ('Cutting', 'Framing', 'Assembly', 'Shipping')
GROUP BY CAST(r.OrderNo AS INT)

出力(間違っています!):

この:

SELECT 
    CAST(r.OrderNo AS INT) AS OrderNo,
        (
        SELECT 
            COUNT(r1.PartNo)
        FROM OrderRouting r1 JOIN OrderRouting r ON r1.OrderNo = r.OrderNo
        WHERE r1.Status = 'Current'
            AND r1.WorkCntr = 'Shipping'
        ) AS [Pcs Complete]
FROM OrderRouting r 
    JOIN OrderDet od ON r.JobNo = od.JobNo
    JOIN Orders o ON od.OrderNo = o.Orderno
WHERE r.Status = 'Current'
        AND r.WorkCntr IN ('Cutting', 'Framing', 'Assembly', 'Shipping')
GROUP BY CAST(r.OrderNo AS INT)

どちらも機能せず、両方とも、外部クエリのOrderNo列にまったく関係のない1つの集計がボード全体に適用されて返されました。おそらく別のことを試してみる必要があるかもしれませんが、必要なことを達成するための最善の方法についてのガイダンスが必要です。事前にTHX

回答 2 件
  • 左結合を使用して、既存のすべての注文番号を表示できると思います。

    SELECT CAST(r.OrderNo AS INT) AS OrderNo,
        r2.[Pcs Complete]
    FROM OrderRouting r 
    JOIN OrderDet od ON r.JobNo = od.JobNo
    JOIN Orders o ON od.OrderNo = o.Orderno
    LEFT JOIN
        (
            SELECT CAST(r1.OrderNo AS INT) OrderNo,
                COUNT(r1.PartNo) AS [Pcs Complete]
            FROM OrderRouting r1
            WHERE r1.Status = 'Current'
                AND r1.WorkCntr = 'Shipping'
            GROUP BY r1.OrderNo
        ) AS [r2] ON r2.OrderNo = r.OrderNo
    WHERE r.Status = 'Current'
    AND r.WorkCntr IN ('Cutting', 'Framing', 'Assembly', 'Shipping')
    GROUP BY CAST(r.OrderNo AS INT)
    
    

  • あなたが何をしようとしているのか完全にはわかりませんが、以下のコードは正しい道を歩むと思います:

    SELECT 
        CAST(r.OrderNo AS INT) AS OrderNo
    FROM OrderRouting r 
        JOIN OrderDet od ON r.JobNo = od.JobNo
        JOIN Orders o ON od.OrderNo = o.Orderno
        JOIN
        (
                SELECT r1.OrderNo,
                COUNT(r1.PartNo)
            FROM OrderRouting r1 JOIN OrderRouting r ON r1.OrderNo = r.OrderNo
            WHERE r1.Status = 'Current'
                AND r1.WorkCntr = 'Shipping'
        ) AS [PCsComplete] ON PcsComplete.OrderNo = r.OrderNo
    WHERE r.Status = 'Current'
            AND r.WorkCntr IN ('Cutting', 'Framing', 'Assembly', 'Shipping')
    GROUP BY CAST(r.OrderNo AS INT)
    
    

あなたの答え