I have following data from banking system: date, product code, due date.
What I need to do is to populate for each product due_dates
backwards i.e. for previous months but only on condition that due_date <= date.
I would also like to populate max_date that is date for the most recent due_date within each product.
As you can see, I may have many due_dates within one product or none.
If there is more than one date, previous shouldn't be overwritten.
This is what it looks like now:
date product due_date
------------------------------------------
2012-01-31 a1
2012-02-28 a1
2012-01-31 b1
2012-02-28 b1
2012-03-31 b1
2012-04-30 b1 2012-02-18
2012-05-31 b1
2012-06-30 b1 2012-05-31
2012-07-31 b1
2012-08-31 b1
2012-09-30 b1 2012-09-01
2012-10-31 b1
2012-04-30 c1
2012-05-31 c1
2012-06-30 c1 2011-03-01
2012-07-31 c1
and this is the result I would like to get:
date product due_date max_date
--------------------------------------------------------
2012-01-31 a1
2012-02-28 a1
2012-01-31 b1 2012-09-30
2012-02-28 b1 2012-02-18 2012-09-30
2012-03-31 b1 2012-02-18 2012-09-30
2012-04-30 b1 2012-02-18 2012-09-30
2012-05-31 b1 2012-05-31 2012-09-30
2012-06-30 b1 2012-05-31 2012-09-30
2012-07-31 b1 2012-09-30
2012-08-31 b1 2012-09-30
2012-09-30 b1 2012-09-01 2012-09-30
2012-10-31 b1 2012-09-30
2012-04-30 c1 2011-03-01 2012-06-30
2012-05-31 c1 2011-03-01 2012-06-30
2012-06-30 c1 2011-03-01 2012-06-30
2012-07-31 c1 2012-06-30