Skip to content
Advertisement

With PostgreSQL how to determine the average completion time given two time stamps while accounting for nulls

In my PostgreSQL database I have an invitations table with the following fields:

I am working to write a PostgreSQL query that tells me the average completion time for invitations per weekly cohort:

Here is what I have so far:

The problem with my query is this line:

The problem is this select statement is not ignoring invitations that were not completed, meaning i.completed_at is NULL

How can I update the above to only include invitations that were completed?

Advertisement

Answer

How do you want to treat them? You can give them the current timestamp:

If you want to ignore them, then your version does that:

When i.completed_at is NULL, then the result is NULL and avg() ignores NULL values.

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