Skip to content
Advertisement

query to get data between date ranges and the padded data around

suppose this is my data in a table in the database

and this is my query to fetch data

I would also like the query to return one date before and after this range. so like the dates padded.

Now how can i do this efficiently. One way could be to get the dates in the ranges and then get all the data where they are less then min date and get the highest datetime of those and add to main range table also repeating this process for the max date. is there any other way?

Advertisement

Answer

You can use lead() and lag():

Note: This uses default values to simplify the logic.

Here is a db<>fiddle. Based on the results you specified, I changed the last comparison to <= from <.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement