Skip to content
Advertisement

PostgreSQL – Get count of items in a table grouped by a datetime column for N intervals

I have a User table, where there are the following fields.

I want to filter this table so that I can get the number of users who have been created in a datetime range, separated into N intervals. e.g. for users having created_at in between 2019-01-01T00:00:00 and 2019-01-02T00:00:00 separated into 2 intervals, I will get something like this.

Is it possible to do so in one hit? I am currently using my Django ORM to create N date ranges and then making N queries, which isn’t very efficient.

Advertisement

Answer

Generate the times you want and then use left join and aggregation:

EDIT:

If you want to specify the number of rows, you can use something similar:

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