I have a requirement where I need to go back to previous values for a column until 1000 rows and get those previous 1000 dates for my next steps, but all those 1000 previous dates are not present for that column in the table. But I need those missing dates to get from output of the query.
When I try to run below query it is not displaying 1000 previous date values from current date.
Example: let's say only 2 dates are available for date column
date
2019-01-16
2019-01-19
I have come up with a query to get back 1000 dates but it is giving only nearest date as all previous back dates are missing
SELECT date FROM table1 t
WHERE
date >= date_sub(current_date,1000) and dt<current_date ORDER BY date LIMIT 1
If I run above query it is displaying 2019-01-16
, since previous 1000 days back date are not present it is giving nearest date ,which is 2019-01-16
but I need missing dates starting from 2016-04-23
(1000th date from current date) till before current date (2019-01-18
) as output of my query.
You can generate dates for required range in the subquery (see
date_range
subquery in the example below) andleft join
it with your table. If there is no record in your table on some dates, the value will be null, dates will be returned from thedate_range
subquery without gaps. Setstart_date
andend_date
parameters for date_range required: