Skip to content
Advertisement

How to check how many issues are opened daily in SQL

I have data from issues in a table with the following schema.

I want to know how many issues are open every day using PostgreSQL. We consider an issue being open at date x if:

  • x >= created_at
  • x <= deleted_at

There may be days where no issues were created or deleted, as in the example. How can I do the query to obtain how many issues are open every day?

Advertisement

Answer

A simple method uses generate_series():

Here is a db<>fiddle.

The above skips dates with no issues. One way to fix this would be to generate all dates and then:

However, the first method should be more efficient if this is not necessary.

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