Each User's First Touch
Kindled's behavioural analyst wants to know what each shopper did the moment they arrived — the very first thing the tracker recorded for them. The assumption everybody makes is that it is always the registration, and the point of the exercise is to find out whether that assumption survives contact with the data. Two actions by the same person can land on the same calendar day, in which case the lower row id is the earlier one.
events — one row per tracked action. user_id points at a row in users. event_type is one of signup, view_product, add_to_cart and purchase. event_date is the day the action happened. Nothing guarantees a person produces all four kinds, or any particular number of rows.
| id | user_id | event_type | event_date |
|---|---|---|---|
| 1 | 1 | signup | 2023-05-01 |
| 2 | 1 | view_product | 2023-05-01 |
| 3 | 1 | add_to_cart | 2023-05-02 |
| 4 | 1 | purchase | 2023-05-03 |
| 5 | 2 | signup | 2023-05-02 |
| 6 | 2 | view_product | 2023-05-02 |
| 7 | 3 | signup | 2023-05-03 |
| 8 | 3 | view_product | 2023-05-03 |
| 9 | 3 | add_to_cart | 2023-05-04 |
| 10 | 4 | signup | 2023-05-04 |
| 11 | 4 | view_product | 2023-05-05 |
| 12 | 4 | add_to_cart | 2023-05-05 |
| 13 | 4 | purchase | 2023-05-06 |
| 14 | 5 | signup | 2023-05-05 |
| 15 | 6 | signup | 2023-05-06 |
| 16 | 6 | view_product | 2023-05-06 |
| 17 | 7 | signup | 2023-05-07 |
| 18 | 7 | view_product | 2023-05-07 |
| 19 | 7 | add_to_cart | 2023-05-08 |
| 20 | 7 | purchase | 2023-05-09 |
| 21 | 8 | signup | 2023-05-08 |
The table already exists in the database — there is nothing to create or load.
Task: Write a query that returns two columns, user_id and first_action, one row per person, first_action being the event_type of their earliest tracked action. Earliest means the smallest event_date, and if a person has two actions on the same day, the one with the smaller id. Rows come back in ascending user_id order.
Example output — shape only, on invented people. first_action holds whichever event_type happens to sit earliest for that person, so it need not be the same label for everybody:
| user_id | first_action |
|---|---|
| 21 | signup |
| 24 | view_product |
| 27 | signup |
| 29 | add_to_cart |
Sign in to solve this problem
Reading problems is free for everyone — solving them (Run, Submit, and tracking what you've solved) needs an account.
Sign in