0

同じデータベースから 4 つのテーブルを結合しています。私のクエリは、私が求めているデータを返していますが、一部のレコードに対して複数の行も返しています。各 WKO_WorkOrderID の最新の LastReportedHrsDate を含む行のみが必要です。誰かがフィルタリングする正しいコードを手伝ってくれますか?

コード(私はまだSQLにかなり慣れていません..):

SELECT     dbo.PLT.PLT_ItemID, SUM(dbo.PLD.PLD_IssueQty) AS Issued, dbo.WKO.WKO_WorkOrderID, dbo.WKO.WKO_RequiredQty, dbo.WKO.WKO_CompleteQty, 
                      dbo.WKO.WKO_RequiredQty - dbo.WKO.WKO_CompleteQty AS openbalance, dbo.PLT.PLT_QtyPerAssy, 
                      (dbo.WKO.WKO_CompleteQty * dbo.PLT.PLT_QtyPerAssy - SUM(dbo.PLD.PLD_IssueQty)) * - 1 AS WIP, dbo.WKO.WKO_ItemID, dbo.WOO.WOO_StatusCode, 
                      dbo.WOO.WOO_LastReportedHrsDate, dbo.WOO.WOO_WorkCenterID
FROM         dbo.PLT INNER JOIN
                      dbo.PLD ON dbo.PLT.PLT_RecordID = dbo.PLD.PLD_PLT_RecordID INNER JOIN
                      dbo.WKO ON dbo.PLT.PLT_WorkOrderID = dbo.WKO.WKO_WorkOrderID LEFT OUTER JOIN
                      dbo.WOO ON dbo.PLT.PLT_WorkOrderID = dbo.WOO.WOO_WorkOrderID
WHERE     (dbo.WKO.WKO_StatusCode = N'Released') AND (dbo.PLT.PLT_ItemID = '9005-20-1794')
GROUP BY dbo.PLT.PLT_ItemID, dbo.WKO.WKO_WorkOrderID, dbo.WKO.WKO_RequiredQty, dbo.WKO.WKO_CompleteQty, dbo.PLT.PLT_QtyPerAssy, dbo.WKO.WKO_ItemID, 
                      dbo.WOO.WOO_StatusCode, dbo.WOO.WOO_LastReportedHrsDate, dbo.WOO.WOO_WorkCenterID
HAVING      (SUM(dbo.PLD.PLD_IssueQty) = dbo.WKO.WKO_RequiredQty * dbo.PLT.PLT_QtyPerAssy) AND (dbo.WOO.WOO_LastReportedHrsDate IS NOT NULL) AND 
                      (dbo.WOO.WOO_StatusCode IS NOT NULL)
ORDER BY dbo.WKO.WKO_WorkOrderID

結果の写真を投稿できません

4

1 に答える 1

1

Please try this:

SELECT      max(dbo.WOO.WOO_LastReportedHrsDate),dbo.PLT.PLT_ItemID, SUM(dbo.PLD.PLD_IssueQty) AS Issued, dbo.WKO.WKO_WorkOrderID, dbo.WKO.WKO_RequiredQty, dbo.WKO.WKO_CompleteQty, 
                      dbo.WKO.WKO_RequiredQty - dbo.WKO.WKO_CompleteQty AS openbalance, dbo.PLT.PLT_QtyPerAssy, 
                      (dbo.WKO.WKO_CompleteQty * dbo.PLT.PLT_QtyPerAssy - SUM(dbo.PLD.PLD_IssueQty)) * - 1 AS WIP, dbo.WKO.WKO_ItemID, dbo.WOO.WOO_StatusCode, 
                      dbo.WOO.WOO_WorkCenterID
FROM         dbo.PLT INNER JOIN
                      dbo.PLD ON dbo.PLT.PLT_RecordID = dbo.PLD.PLD_PLT_RecordID INNER JOIN
                      dbo.WKO ON dbo.PLT.PLT_WorkOrderID = dbo.WKO.WKO_WorkOrderID LEFT OUTER JOIN
                      dbo.WOO ON dbo.PLT.PLT_WorkOrderID = dbo.WOO.WOO_WorkOrderID
WHERE     (dbo.WKO.WKO_StatusCode = N'Released') AND (dbo.PLT.PLT_ItemID = '9005-20-1794')
AND (dbo.WOO.WOO_LastReportedHrsDate IS NOT NULL) AND (dbo.WOO.WOO_StatusCode IS NOT NULL)
GROUP BY dbo.PLT.PLT_ItemID, dbo.WKO.WKO_WorkOrderID, dbo.WKO.WKO_RequiredQty, dbo.WKO.WKO_CompleteQty, dbo.PLT.PLT_QtyPerAssy, dbo.WKO.WKO_ItemID, 
                      dbo.WOO.WOO_StatusCode, dbo.WOO.WOO_WorkCenterID
HAVING      (SUM(dbo.PLD.PLD_IssueQty) = dbo.WKO.WKO_RequiredQty * dbo.PLT.PLT_QtyPerAssy) 
ORDER BY dbo.WKO.WKO_WorkOrderID
于 2013-08-29T20:45:48.540 に答える