Below is a mysql table which has sparse dates.
col dt_id value
A1 2018-05-28 30
A1 2018-05-30 20
A1 2018-05-31 50
A1 2018-06-01 50
A1 2018-06-04 80
A1 2018-06-05 50
The output should be something like below where missing dates are populated along with the last value.
col dt_id value
A1 2018-05-28 30
A1 2018-05-29 30
A1 2018-05-30 20
A1 2018-05-31 50
A1 2018-06-01 50
A1 2018-06-02 50
A1 2018-06-03 50
A1 2018-06-04 80
A1 2018-06-05 50
here the following were generated.
A1 2018-05-29 30
A1 2018-06-02 50
A1 2018-06-03 50
I know solutions with oracle using last_value() over (partition by.., but since this is mysql, its a bit tricky.
Here is what i've tried:
create a time table and populate with data:
CREATE TABLE `time_table` (date_id date not null);
create table ints ( i tinyint ); insert into ints values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
insert into time_table (date_id) select date('2016-09-01')+ interval a.i*10000 + b.i*1000 + c.i*100 + d.i*10 + e.i day
from ints a
join ints b
join ints c
join ints d
join ints e
where (a.i*10000 + b.i*1000 + c.i*100 + d.i*10 + e.i) <= 11322 order by 1;
select * from time_table limit 10;
+------------+
| date_id |
+------------+
| 2018-09-22 |
| 2018-09-21 |
| 2018-09-20 |
| 2018-09-19 |
| 2018-09-18 |
| 2018-09-17 |
| 2018-09-16 |
| 2018-09-15 |
| 2018-09-14 |
| 2018-09-13 |
+------------+
Here is the data for the balance table:
Here is the data
+------+------------+-------+
| A1 | 2018-05-28 | 30 |
| A1 | 2018-05-30 | 20 |
| A1 | 2018-05-31 | 50 |
| A1 | 2018-06-01 | 50 |
| A1 | 2018-06-04 | 80 |
| A1 | 2018-06-05 | 50 |
| B1 | 2018-05-28 | 30 |
| B1 | 2018-05-30 | 20 |
| B1 | 2018-05-31 | 50 |
| B1 | 2018-06-01 | 50 |
| B1 | 2018-06-04 | 80 |
| B1 | 2018-06-05 | 50 |
| C1 | 2018-05-28 | 30 |
| C1 | 2018-05-30 | 20 |
| C1 | 2018-05-31 | 50 |
| C1 | 2018-06-01 | 50 |
| C1 | 2018-06-04 | 80 |
| C1 | 2018-06-05 | 50 |
| D1 | 2018-06-28 | 30 |
| D1 | 2018-07-02 | 20 |
| D1 | 2018-07-04 | 50 |
| D1 | 2018-07-08 | 80 |
| D1 | 2018-07-19 | 50 |
+------+------------+-------+
mysql> select b.id, ab.id, tt.`date_id` as cal_date, b.`mx` as ex_date, val
-> from time_table tt
-> inner join (select id, min(date_id) mi, max(date_id) mx from balance group by id) b
-> on tt.`date_id` >= b.`mi`
-> and tt.`date_id` <= b.mx
-> left join (select id, date_id, sum(value) val from balance group by id, date_id) ab
-> on ab.id = b.id and tt.`date_id` = ab.date_id
-> order by cal_date;
+------+------+------------+------------+------+
| id | id | cal_date | ex_date | val |
+------+------+------------+------------+------+
| A1 | A1 | 2018-05-28 | 2018-06-05 | 30 |
| A1 | NULL | 2018-05-29 | 2018-06-05 | NULL |
| A1 | A1 | 2018-05-30 | 2018-06-05 | 20 |
| A1 | A1 | 2018-05-31 | 2018-06-05 | 50 |
| A1 | A1 | 2018-06-01 | 2018-06-05 | 50 |
| A1 | NULL | 2018-06-02 | 2018-06-05 | NULL |
| A1 | NULL | 2018-06-03 | 2018-06-05 | NULL |
| A1 | A1 | 2018-06-04 | 2018-06-05 | 80 |
| A1 | A1 | 2018-06-05 | 2018-06-05 | 50 |
+------+------+------------+------------+------+