Skip to content
Advertisement

SQL – count rows between dynamic number of date ranges

I have a table to store events like this:

and another table to store posts like this:

if I want to get count of posts that happened during a single event I can write:

but how can I get the count of posts that happened during multiple events, when the target events are only known at runtime?

Edit:

the database is PostgresSQL 13

Advertisement

Answer

A join plus aggregation approach should work here:

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