Reputation: 137
I have a table of time-series data that has some gaps in the series. An example of the data is below:
Date | Value |
---|---|
2022-11-17 | 1 |
2022-11-14 | 2 |
I want to insert rows for the dates between the existing rows (2022-11-15, 2022-11-16) that have the value of the latest date before the date being inserted (the 2022-11-14 row).
I started by using an imperative solution in my application programming language but I'm convinced there must be a way to do this in SQL.
Upvotes: 0
Views: 358
Reputation: 23726
INSERT INTO mytable -- 5
SELECT
generate_series( -- 1
mydate + 1, -- 2
lead(mydate) OVER (ORDER BY mydate) - 1, -- 3
interval '1 day'
)::date as gs,
t.myvalue -- 4
FROM mytable t;
generate_series()
to generate date serieslead()
window function is used to access the next rowUpvotes: 1