mysql - SQL - Explode the dates between the ranges as new records -
i want perform explode kind of task in mysql, need create new records per day start , end dates.
current data looks this:
person, start date, end date, score , 12/05/2017, 14/05/2017, 0.8 , 15/05/2017, 17/07/2017, 0.3 b , 12/05/2017, 14/05/2017, 0.5 b , 15/05/2017, 17/07/2017, 0.2 now, need re create data as
person, date, score , 12/05/2017, 0.8 , 13/05/2017, 0.8 , 14/05/2017, 0.8 , 15/05/2017, 0.3 , 16/05/2017, 0.3 , 17/05/2017, 0.3 b , 12/05/2017, 0.5 b , 13/05/2017, 0.5 b , 14/05/2017, 0.5 b , 15/05/2017, 0.2 b , 16/05/2017, 0.2 b , 17/05/2017, 0.2 since, joining tables other tables on date & person column, operation needed. how can in mysql?
you can this
select person, startdate + interval q.n - 1 day dates,score table1 t cross join ( select a.n + b.n * 10 + 1 n (select 0 n union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) ,(select 0 n union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) b order n ) q q.n - 1 <= datediff(enddate, startdate) order pno, dates the subquery generates sequence of numbers 1 100. can adjust needs (if date differences span more or less 100 days) or substitute persisted tally(numbers) table if lot of such queries.
Comments
Post a Comment