I use pySpark to process website visitor datasets, where each user is assigned a unique identifier.
| Visit timestamp | User id |
|---|---|
| 2022-01-01 10:23:44.123456 | aaa |
| 2022-01-02 11:22:44.123456 | aaa |
| 2022-01-01 11:00:44.123456 | bbb |
| 2022-01-01 12:22:44.123456 | bbb |
| 2022-01-05 13:23:44.123456 | abc |
| 2022-01-03 14:22:44.123456 | ccc |
| 2022-01-04 10:23:44.123456 | ddd |
| 2022-01-01 11:22:44.123456 | eee |
I am searching for an efficient way to count the number of distinct user_ids between all possible date combinations, looking for an output in the following form:
| Start date | End date | unique visitors |
|---|---|---|
| 2022-01-01 | 2022-01-01 | 3 |
| 2022-01-01 | 2022-01-02 | 3 |
| 2022-01-01 | 2022-01-03 | 4 |
| 2022-01-01 | 2022-01-04 | 5 |
| 2022-01-01 | 2022-01-05 | 6 |
| 2022-01-02 | 2022-01-02 | 1 |
| 2022-01-02 | 2022-01-03 | 2 |
| 2022-01-02 | 2022-01-04 | 3 |
Additionally, would need to store intermediary data in such a way that this can be run incrementally as new data comes in.
I can easily use the approx_count_distinct function to compute the number of distinct visitors for any given day, but that does not solve the start / end data and incremental load needs.
Thanks!