I have two tables: Table1
has an account number and a description, and is static and contains all existing accounts.
Table2
has an account number, a period id, a beginning and an ending balance.
I have joined the tables on the account number. I will use account number 0000-1234-000 for my example. It has data for periods 1,2,3 and 5.
My query is simple:
SELECT t2.periodid, t1.acctnmbr, t1.description, t2.bgnblnc, t2.endblnc
FROM table1 T1
LEFT JOIN Table2 T2 on t1.acctnmbr = t2.acctnmbr
WHERE t1.acctnmbr = 0000-1234-000
This returns the following:
1 0000-1234-000 Disbursement 256,786.00 165,784.00
2 0000-1234-000 Disbursement 165,784.00 184,574.00
3 0000-1234-000 Disbursement 184,574.00 376,144.00
5 0000-1234-000 Disbursement 376,144.00 165,784.00
You can see period 4 is missing. No data was entered for this account in Table2 for period 4. I would have thought that with the left join I would at leas have returned the account number with "nulls" for values but that is a question for another day. What I want is to insert period 4 in my query with the results of period 3.
So the logic is "if not exists then previous period balance". How can I do this?