Skip to content
Advertisement

Postgress fill the missed row values with previous values during query

I have some table which has some rows where data is not exists, due to some biz reason I can not show the null or 0 to user during those days, so need to keep previous value of that table.

I want the data like below when I query a table, the missed date should be added with value of previous date

I am using JPA to query the table

@Query(value = “SLECT * FROM Foo where ID=:uid”) Lits getFoo(String uid);

Advertisement

Answer

Recursive CTEs are a pretty easy way to fill-in-the-gaps like this:

They make it easy to retain the values you want from the previous row.

The most efficient method, though, is probably to use generate_series() — but within each row:

Here is a db<>fiddle.

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