select TestA.CID, (case when QTY2 != QTY then QTY2 else QTY end ) as QTYfrom TestA left join TestB on TestA.CID = TestB.CID