DECLARE cursor1 CURSOR AS Select mwtrn.[Chart Number], mwtrn.[Case Number], mwtrn.[Entry Number], mwtrn.[Amount], Round(Sum(mwpax.[Payment Amount]), 4) totApplied FROM MWTRN Left Outer Join MWPAX on mwpax.[payment reference] = [entry number] where [Transaction Type] >= 'I' and [Transaction Type] <> 'P' Group by [Chart Number], [Case Number], [Entry Number], [Amount]; OPEN cursor1; CREATE TABLE #TEMP (ChartNumber Char(8), CaseNumber Integer, EntryNumber Integer, Unapplied Double); WHILE FETCH cursor1 DO IF (cursor1.totApplied - cursor1.[Amount] <> 0) or (cursor1.totApplied is null) THEN INSERT INTO #TEMP VALUES(cursor1.[Chart Number], cursor1.[Case Number], cursor1.[Entry Number], cursor1.[Amount] - cursor1.totApplied ); ENDIF; ENDWHILE; SELECT * FROM #TEMP;