Skip to content
Advertisement

SQL how can we get monthly trend from 2 separated columns start_date and end_date?

Imagine we have the following data:

The question here is to get the 2020 monthly trend of active paid subscriptions. For each subscriber (ID), we can only count the months that they are active. So for S1, we can only count S1 active in Jan 2020 and March 2020, not Feb 2020.

During the interview, I wrote a function and said we can loop over to call this function for each month in 2020

or

I wonder if there is any better way to write this SQL query? Thanks!

Advertisement

Answer

One method uses a table of numbers:

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