Hi all,
I have the following CTE query which I need to convert to a nested query. Any help in converting is appreciated.
I have the following CTE query which I need to convert to a nested query. Any help in converting is appreciated.
Code:
WITH InvAtA AS (
SELECT SUM(Lin.qty) AS qtyOut, Lin.POLineID, Led.locationID
FROM dbo.tblTRLedger AS Led INNER JOIN dbo.tblTRLine AS Lin ON Led.TRLedgerID = Lin.TRLedgerID
GROUP BY Lin.POLineID, Led.locationID
HAVING (Led.locationID = 1)),
InvAtO AS (
SELECT Ledger.POLedgerID, Line.transDesc AS chopMark, SUM(Line.unitQty) AS qtyIn, Line.POLineID, Ledger.positionID
FROM dbo.tblPOLedger AS Ledger RIGHT OUTER JOIN dbo.tblPOLine AS Line ON Ledger.POLedgerID = Line.POLedgerID
GROUP BY Ledger.POLedgerID, Line.transDesc, Line.POLineID, Ledger.positionID
HAVING (Ledger.positionID = 1))
SELECT ino.qtyIn - ISNULL(out.qtyOut, 0) AS quantity, ino.POLedgerID
FROM InvAtO AS ino LEFT OUTER JOIN
InvAtA AS out ON ino.POLineID = out.POLineID
WHERE (ino.qtyIn - ISNULL(out.qtyOut, 0) > 0)