SP
From test
inner join test as test1 using id
Array join range(5) as d
Where test.Date <= toDate('2021-01-01') + toIntervalDay(d) and test1.Date <= toDate('2021-01-01') + toIntervalDay(d)
group by id, Val, Date, dt
having max(test1.Date) = Date
Order by dt, id;