I'm working with the following table where you can get activity from customer purchases.
DateOfActivity | CustomerReference | Reference Line | Description | Receivable Amount |
---|---|---|---|---|
24/10/2022 | CUST567 | 1 | Credit Purchase | 20,000 |
24/10/2022 | CUST567 | 4 | Credit Purchase | 10,000 |
24/10/2022 | CUST555 | 2 | Credit Purchase | 50,000 |
27/10/2022 | CUST555 | 2 | Contract Sign | 0 |
27/10/2022 | CUST567 | 4 | Contract Sign | 0 |
27/10/2022 | CUST567 | 1 | Contract Sign | 0 |
27/10/2022 | CUST567 | 4 | Repayment | -3,500 |
27/10/2022 | CUST567 | 4 | Repayment | -6,500 |
13/11/2022 | CUST567 | 1 | Repayment | -10,000 |
13/11/2022 | CUST567 | 1 | Repayment | -2,000 |
18/11/2022 | CUST567 | 1 | Contract Sign | 0 |
18/11/2022 | CUST567 | 1 | Repayment | -3,000 |
I'm using the following query to extract the above table:
Select
DateOfActivity, CustomerReferencce, ReferenceLine, Description, ReceivableAmount
From 'Table A'
Where
DateOfActivity >= '2022-09-01'
Group by
DateOfActivity
As you can see that the table will only get bigger because more customer activity is being added. How can I change my query so the customers who have fully paid their receivable amount don't show up in this table?
The result from the above script change that I am expecting is as follows:
DateOfActivity | CustomerReference | Reference Line | Description | Receivable Amount |
---|---|---|---|---|
24/10/2022 | CUST567 | 1 | Credit Purchase | 20,000 |
24/10/2022 | CUST555 | 2 | Credit Purchase | 50,000 |
27/10/2022 | CUST555 | 2 | Contract Sign | 0 |
27/10/2022 | CUST567 | 1 | Contract Sign | 0 |
13/11/2022 | CUST567 | 1 | Repayment | -10,000 |
13/11/2022 | CUST567 | 1 | Repayment | -2,000 |
18/11/2022 | CUST567 | 1 | Contract Sign | 0 |
18/11/2022 | CUST567 | 1 | Repayment | -3,000 |
CUST567 Reference Line 4 has been removed because the sum of his Credit Purchase + Contract Sign + Repayment = $0. All other Customer rows are still showing up.
How can edit the query so this is done automatically for Large data? Please note the following assumptions:
Customer Reference for multiple customers can be the same or different (for example in the above example, CUST567 has two Reference Lines 1 & 4. However, CUST555 only has one reference Line 2.
The data is removed for Customers based on the Receivable amount coming down to Nil (so all rows for that CustomerReference & Reference Line are removed)
Thanks in Advance