I have a General Ledger Table in a database (SQL Server 2005), With columns (ID, Insertion_Date, Invoice_No, Debit, Credit and Balance).
When a user add some entries, he will be given to fill the invoice no. and amount(Debit) so Balance will automatically be added as Balance = Balance - Amount. Anyway, after having multiple rows for the same invoice, the user decided to edit the amount for a previous row. After editing the amount, the balance of the same row and the other rows should change automatically. (As: the edited row will take the new amount like: Balance = Balance - newamount, and the other rows will take the new balance and sub. with their current amount).
Example: Table GL
ID Date Invoice_No Debit Credit Balance
---------------------------------------------------------
1 19/3/2014 123456 0 400 400
2 19/3/2014 123456 100 0 300
3 20/3/2014 123456 50 0 250
4 21/3/2014 123456 100 0 150
5 22/3/2014 123456 50 0 100
After Editing the row with ID 2 and making the change on the Debit Column (instead of 100 making it 50) only, The other rows will be effected so the result should look like this.
ID Date Invoice_No Debit Credit Balance
---------------------------------------------------------
1 19/3/2014 123456 0 400 400
2 19/3/2014 123456 50 0 350
3 20/3/2014 123456 50 0 300
4 21/3/2014 123456 100 0 200
5 22/3/2014 123456 50 0 150
Any Idea how can I manage this with a single query. Am new at SQL and need help. Please ask for more Info. if needed.