SELECT DISTINCT CANCELL.adjustmentId AS CancelID, RECALC.adjustmentId AS RecalcID, CANCELL.companyCode, CANCELL.[Taps account] AS CancelTaps, CANCELL.billReference, RECALC.collateralCurrency, CANCELL.clientCode, CANCELL.validfromdate, Abs(CANCELL!SumOfusdpnlamount+RECALC!SumOfusdpnlamount) AS Diff, CANCELL.SumOfusdpnlamount AS CancelUSD, RECALC.SumOfusdpnlamount AS RecalcUSD, IIf(Left(recalc!eventReference,Len(recalc!eventreference)-1)=Left(cancell!eventReference,Len(cancell!eventreference)-1),"Same","Different") AS EventReferenceSame, Format([StartDate],"dd-mmm-yy") AS [From], Format([EndDate],"dd-mmm-yy") AS [To], RECALC.borrowLoanPledgeReceipt, RECALC.[Taps account] AS RecalcTaps
FROM BillingAdjustments AS CANCELL INNER JOIN BillingAdjustments AS RECALC ON (CANCELL.Flag = RECALC.Flag) AND (CANCELL.collateralCurrency = RECALC.collateralCurrency) AND (CANCELL.companyCode = RECALC.companyCode) AND (CANCELL.billReference = RECALC.billReference) AND (CANCELL.validfromdate = RECALC.validfromdate) AND (CANCELL.clientCode = RECALC.clientCode) AND (CANCELL.borrowLoanPledgeReceipt = RECALC.borrowLoanPledgeReceipt)
GROUP BY CANCELL.adjustmentId, RECALC.adjustmentId, CANCELL.companyCode, CANCELL.[Taps account], CANCELL.billReference, RECALC.collateralCurrency, CANCELL.clientCode, CANCELL.validfromdate, Abs(CANCELL!SumOfusdpnlamount+RECALC!SumOfusdpnlamount), CANCELL.SumOfusdpnlamount, RECALC.SumOfusdpnlamount, IIf(Left(recalc!eventReference,Len(recalc!eventreference)-1)=Left(cancell!eventReference,Len(cancell!eventreference)-1),"Same","Different"), Format([StartDate],"dd-mmm-yy"), Format([EndDate],"dd-mmm-yy"), RECALC.borrowLoanPledgeReceipt, RECALC.[Taps account], Left([recalc]![comments],6), Left([cancell]![comments],7)
HAVING (((CANCELL.validfromdate)>=[StartDate] And (CANCELL.validfromdate)<=[EndDate]) AND ((Left([recalc]![comments],6))="RECALC") AND ((Left([cancell]![comments],7))="CANCELL") AND ((Abs([CANCELL]![SumOfusdpnlamount]+[RECALC]![SumOfusdpnlamount]))<20000)) OR (((Left([recalc]![comments],6))="RECALC") AND ((Left([cancell]![comments],7))="CANCELL") AND ((Abs([CANCELL]![SumOfusdpnlamount]+[RECALC]![SumOfusdpnlamount]))<20000));