Quantcast
Channel: SQLServerCentral » SQL Server 2014 » Development - SQL Server 2014 » Latest topics
Viewing all articles
Browse latest Browse all 3145

Credits and Debits

$
0
0
Hello,This is my first post here , so please be patient.I am trying to build a logic in SQL server 2012 where the Debit amount should always be assigned to the Previous credit amount which still has an outstanding balance.(unable to format this table any better)TransactionID Customer TransactionType Date Amount CreditID1 1 Credit 1/1/16 -20 12 1 Debit 1/2/16 20 3 1 Credit 1/3/16 -100 24 1 Debit 1/4/16 50 5 1 Debit 1/5/16 50 6 1 Credit 1/6/16 -50 37 1 Debit 1/7/16 20 8 1 Credit 1/8/16 -50 49 1 Debit 1/7/16 20So in the example above Debit 20$ ( Transaction ID 2) should be assigned to CreditID1and TransactionId 4 and 5 should be assigned to CreditID 2TransactionID 7 and 9 should be assigned to CreditID 3and Credit 4 should show remaining balance as -50 $I used the gap and Island approach here , but the problem is using that I was able to assign the debit to the last credit ID . It messes my logic when there a Credit happens before the earlier credit is net to 0.( ex Credit rank 3 and Credit rank 4)Please let me know if my question is not clear. Happy to explain. Part of me thinks I should be using recursive CTE's , but not 100% sure how it can help me here.

Viewing all articles
Browse latest Browse all 3145

Trending Articles