I have a table called 'users' that has the following structure:
| id (PK) | campaign_id | createdAt |
|---|---|---|
| 1 | 123 | 2022-07-14T10:30:01.967Z |
| 2 | 1234 | 2022-07-14T10:30:01.967Z |
| 3 | 123 | 2022-07-14T10:30:01.967Z |
| 4 | 123 | 2022-07-14T10:30:01.967Z |
At the same time I have a table that tracks clicks per user:
| id (PK) | user_id(FK) | createdAt |
|---|---|---|
| 1 | 1 | 2022-07-14T10:30:01.967Z |
| 2 | 2 | 2022-07-14T10:30:01.967Z |
| 3 | 2 | 2022-07-14T10:30:01.967Z |
| 4 | 2 | 2022-07-14T10:30:01.967Z |
Both of these table are up to millions of records... I need the most efficient query to group the data per campaign_id.
The result I am looking for would look like this:
| campaign_id | total_users | total_clicks |
|---|---|---|
| 123 | 3 | 1 |
| 1234 | 1 | 3 |
I unfortunately have no idea how to achieve this while minding performance and most important of it all I need to use WHERE or HAVING to limit the query in a certain time range by createdAt