Hi There.
I have tried to get the solution from ChatGPT but it has not worked. I think I need real people to help.
I have a transaction table made up of
RecordId Int
MatterId Int
Date DateTime
Amount Decimal(13.2)
I need a script that will by MatterId, Date find the latest positive transaction per MatterId of a Select list.
If the latest transaction is a Positive then look no further
If the latest transaction is a Negative look for the previous positive transaction of the same amount by Date desc, RecordId desc and exclude those two transactions, then repeat the procedure until a Positive transaction is found then stop.
The following are the two tables:
Matter Table
RecordId FileRef
12599 AMA5/0126
Transaction Table
RecordId MatterId Date Amount
1 12599 05/12/2023 88.50
2 12599 05/12/2023 -88.50
3 12599 05/01/2024 88.50
4 12599 05/02/2024 88.50
5 12599 05/03/2024 88.50
6 12599 04/04/2024 88.50
7 12599 06/05/2024 88.50
8 12599 04/06/2024 88.50
9 12599 28/06/2024 -88.50
10 12599 28/06/2024 -88.50
Select
m.FileRef,
cd.Date,
cd.Amount
From Matter m
Left Join ColDebit cd On cd.CD_MatterId = m.M_RecordId
I want to change the Left Join ColDebit to look for the latest positive transaction. If the latest transaction is negative it must be matched against the latest positive and removed first.
e.g.
The last Transaction 10 is negative so it must be matched against Transaction 8 because it is the first positive transaction.
Transaction 9 is negative so it must be matched against 7 because it is the first positive transaction after the previous transaction has been ellimated.
The result for this Matter 12599 should be transaction 6 which is dated 04/04/2024 and amount is 88.50.