Skip to content
Advertisement

Filling missing dates in each group while querying data from PostgreSQL

I have data in my table of the following form:

Now while querying this data, I want the missing dates to be filled with null values.

In PostgreSQL, I can use generate_series() to create a series of dates and then left join with it. But that only works if I have just a series of dates and values. In this case, since I have two types, I don’t really have any missing dates in the series as a whole, but I have missing dates in each group/partition.

Is it possible to use generate_series() with left join to fill rows with null in this case?

Advertisement

Answer

You may cross join with a table which contains all types, and then use the same left join approach you were already considering:

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