Create Table G (Goods Varchar (2), Lot Varchar (3), BAL INT Create Table O (Oid Varchar, Goods Varchar (2), Qty Int) Insert G SELECT 'AA', 'P01', 5 Union Select ' AA ',' P02 ', 10 Union Select' BB ',' P01 ', 20
Insert o SELECT '1', 'AA', 11 Union Select '1', 'BB', 10 Union Select '2', 'AA', 2 Union Select '3', 'AA', 1
SELECT OID, B.GOODS, LOT, DELI = (Case When Sq> SB THEN SB ELSE SQ END) - (Case When SQ-QTY SB-Balorder by Oid, B.Goods, LOT
Drop Table G, O