I have a table that captures account-level subscription events. Assume that there are three event types. Two of these indicate that an account is subscribed, and the last one is synonymous with the act of unsubscribing. These events are emitted as they happen, and there is no "heart beat" at the account level that reports current status on a regular cadence.
However, I need to report on account status on a cadence: how many accounts are subscribed on each day, past and present? Reporting on the present is easy, because I can use any BI tool to find the event type with the max date per account. Reporting on the past is hard, because I need to find the event name with the max timestamp from the relative point of the past date.
I have another table that contains a bunch of date information, with one record per day. I believe that if I join the two tables I can create "artificial" records in the event table for each day in the date table, but I can't figure out the syntax.
I can use Python Pandas to create a dummy example version of the event table:
events = {'date': ['2022-02-25', '2022-02-28', '2022-03-02', '2022-03-05']
, 'account_id': ['asdf', 'asdf', 'asdf', 'asdf']
, 'event_type': ['subscribe', 'modify', 'modify', 'unsubscribe']
}
pd.DataFrame(data = events)
The output of which is this:
date account_id event_type
0 2022-02-25 asdf subscribe
1 2022-02-28 asdf modify
2 2022-03-02 asdf modify
3 2022-03-05 asdf unsubscribe
But the output I need is:
date account_id max_event_type account_status
0 2022-02-25 asdf subscribe subscribed
1 2022-02-26 asdf subscribe subscribed
2 2022-02-27 asdf subscribe subscribed
3 2022-02-28 asdf modify subscribed
4 2022-03-01 asdf modify subscribed
5 2022-03-02 asdf modify subscribed
6 2022-03-03 asdf modify subscribed
7 2022-03-04 asdf modify subscribed
8 2022-03-05 asdf unsubscribe unsubscribed
Here, "account_status" is something I can derive from the events, I only include it as an example of the goal.
So my question is: can I arrive at the desired output, using SQL, if I have the first example output plus a table of dates to join to? And if so, how? Thanks!