How do you loop this with a for loop? The goal is to create a report with payments, charges, credits, and adjustments . Carryover and Total due are obtained through manipulation of the data in the columns.
Problem is: Carryover = Total Due = [Carryover] – [Payment] + [charges] – [credits] + or – [adjustments] for the current month. That is easy on the surface.
“Carryover” = “Total Due” from the previous month, i.e., Month 2’s “Carryover” of 100 is Month 1’s carryover, and Month 3’s “Carryover” of 150 is last month’s “Total Due.”
Neither “Carryover” nor “Total Due” are records from the database, but pulling data from the previous query. At the same time, “Total Due” includes the “Carryover” so that if you don’t this carefully, the queries are circular.
Victor
MO CARRYOVER PAY CHARGES CREDITS ADJUST TOTAL DUE
1 0 0 100 0 0 100
2 100 50 100 0 0 150
3 150 50 100 0 0 200
4 200 50 100 50 0 200
create table payments(
month varchar2(2),
carryover number(10),
payments number(10),
charges number(10),
credits number(10),
adjustment number(10),
total number(10)
)
set X=2
<loop X till x = 13a>
select Month from payments
<if Month = 12>
then X= X-1
insert into payments
(month, Carryover)
select X, total from payments where month = X-1
<else>
insert into payments
(month, Carryover)
select X, total from payments where month = X-1
<endif>
set X = X+1
<Loop>
Problem is: Carryover = Total Due = [Carryover] – [Payment] + [charges] – [credits] + or – [adjustments] for the current month. That is easy on the surface.
“Carryover” = “Total Due” from the previous month, i.e., Month 2’s “Carryover” of 100 is Month 1’s carryover, and Month 3’s “Carryover” of 150 is last month’s “Total Due.”
Neither “Carryover” nor “Total Due” are records from the database, but pulling data from the previous query. At the same time, “Total Due” includes the “Carryover” so that if you don’t this carefully, the queries are circular.
Victor
MO CARRYOVER PAY CHARGES CREDITS ADJUST TOTAL DUE
1 0 0 100 0 0 100
2 100 50 100 0 0 150
3 150 50 100 0 0 200
4 200 50 100 50 0 200
create table payments(
month varchar2(2),
carryover number(10),
payments number(10),
charges number(10),
credits number(10),
adjustment number(10),
total number(10)
)
set X=2
<loop X till x = 13a>
select Month from payments
<if Month = 12>
then X= X-1
insert into payments
(month, Carryover)
select X, total from payments where month = X-1
<else>
insert into payments
(month, Carryover)
select X, total from payments where month = X-1
<endif>
set X = X+1
<Loop>