I'm having some trouble getting just the last payment date to show when there are multiple payments for a single open invoice. The basic idea is to be able to do a customer summary by invoice. I tried to do a distinct Transid, which would really solve this for me, I think, but I'm unable to get that working. Any ideas?
Here's the query I'm using:
SELECT T0.[CardCode], T0.[CardName], T2.[Dscription],T0.[NumAtCard], T0.[DocDate], T2.[LineTotal], T0.[PaidSum], T3.[SumApplied], max(t4.[DocDate]) as 'Payment Date', T1.[Balance], T0.[TransId]
FROM [dbo].[OINV] T0
INNER JOIN OCRD T1 ON T0.[CardCode] = T1.[CardCode]
INNER JOIN INV1 T2 ON T0.[DocEntry] = T2.[DocEntry]
INNER JOIN RCT2 T3 on t3.[doctransid] = t0.[transid] inner join ORCT T4 on t3.[docnum] =t4.[docentry]
WHERE T0.[Installmnt] > 1 AND T0.[DocStatus] = 'O'
GROUP BY t0.cardcode, t0.cardname, t2.dscription, t0.numatcard, t0.docdate, t2.linetotal, t0.paidsum, t3.sumapplied, t4.docdate, t1.balance, t0.transid