I have a table named Payments that contains the following:
ID AcctNo RevCode Amount Group GroupMaxAmt
7 001 360 1000.00 1 3000.00
1 001 361 1500.00 1 3000.00
3 001 362 1600.00 1 3000.00
4 001 710 1500.00 2 3400.00
6 001 711 2000.00 2 3400.00
2 001 712 2100.00 2 3400.00
9 002 360 1600.00 1 3000.00
8 002 361 1650.00 1 3000.00
5 002 490 2550.00 3 2500.00
10 002 491 1000.00 3 2500.00
RevCode 360 to 362 belong to Group 1 for which the maximum amount is 3000.00 for the sum of Amount in the Group falling under each AcctNo. So, for example, AcctNo 001 has RevCode 360 to 362 for which the sum of Amount is 4100.00 which exceeds the maximum amount of 3000.00 for Group 1. I need to update the table so that the sum does not exceed the maximum amount for the Group. The update should reduce the Amount starting with the highest amount until the sum no longer exceeds the maximum amount for the Group.
Here is how the table should look like after the updates are done:
ID AcctNo RevCode Amount Group GroupMaxAmt
7 001 360 1000.00 1 3000.00
1 001 361 1500.00 1 3000.00
3 001 362 500.00 1 3000.00
4 001 710 1500.00 2 3400.00
6 001 711 1900.00 2 3400.00
2 001 712 0.00 2 3400.00
9 002 360 1600.00 1 3000.00
8 002 361 1400.00 1 3000.00
5 002 490 2500.00 3 2500.00
10 002 491 0.00 3 2500.00
Could someone please help me with the SQL script to perform this update? Ideally, the script should not have to use a cursor.
Thank you so much in advance! It would be much appreciated.
BTW, how do I enter a table in a posting so that the spaces I enter between columns are preserved? The table I entered above is hardly legible. Sorry about that.