• Perhaps this works also?

    DECLARE @Store_Out_Details AS TABLE (PurchaseOrderNo CHAR(3), SerialNo CHAR(4), Qty INT);

    INSERT INTO @Store_Out_Details

    VALUES ('001','I000',20),('001','I001',10),('002','I000',50),('003','I002',20);

    DECLARE @Store_PO_Details AS TABLE (IssueNo CHAR(4), SerialNo CHAR(4), Qty INT);

    INSERT INTO @Store_PO_Details

    VALUES('S001','I000',10),('S002','I001',5);

    SELECT SerialNo, Qty=SUM(Qty)

    FROM

    (

    SELECT SerialNo, Qty

    FROM @Store_Out_Details

    UNION ALL

    SELECT SerialNo, -Qty

    FROM @Store_PO_Details

    ) a

    GROUP BY SerialNo;

    With thanks to Cadavre for the set up data!


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St