I'm trying to show payments applied to bills and I'd like to know if there is a way to do this without using cursors and imperative logic. I have a Bills table and Payments table. Payments are not always in the amounts of the bills, sometimes over, sometimes under. I'm trying to create some sort of join that shows how much of each payment was applied to each bill.
Assumptions:
- Bills belong to a single account
- Payments are applied to each bill in order of its ID
Given a Bills table:
ID Amount
1 500
2 500
3 500
Scenario 1:
Payments table
ID Amount
1 750
2 750
With the above Bills table, and the scenario 1 payments table, I would like to see this output:
Bill ID | Payment ID | Amount Applied
1 1 500
2 2 250
3 2 500
3 2 250
Scenario 2:
Payments table:
1 300
2 300
3 300
4 300
5 300
Given above Bills table and scenario 2 Payments table, I would like to see output:
Bill ID | Payment ID | Amount Applied
1 1 300
1 2 200
2 2 100
2 3 300
2 4 100
3 4 200
3 5 300
I can do this with cursors, but I'd like to find out if anyone knows how to do this with set-based SQL.
Thanks!
Aucun commentaire:
Enregistrer un commentaire