Skip to content
Advertisement

Pad row with default if values not found PostgresSQL

I wanted to return the last 7 days of user_activity, but for those empty days I want to add 0 as value

Say I have this table

and this should be the expected output , for the last 7 days

Here is my query so far, I can only get the last 7 days but not sure if it’s positive to add to default values

Advertisement

Answer

You may left join your table with generate_series. First you need to have a way to use the rows for distinct ids. That set can then be correctly joined with the main table.

DEMO

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