Let's say we have a e-shop and all users' events in a single table.
The events table looks like this:
| id | user_id | type | time | comment |
|---|---|---|---|---|
| 1 | 1 | visit_site | 2022-04-07 14:08 | |
| 1 | 1 | add_item_to_cart | 2022-04-07 14:17 | |
| 1 | 1 | add_item_to_cart | 2022-04-07 14:17 | |
| 1 | 1 | checkout | 2022-04-07 14:22 | |
| 1 | 1 | pay | 2022-04-07 14:25 | |
| 1 | 2 | visit_site | 2022-04-07 14:30 | |
| 1 | 2 | add_item_to_cart | 2022-04-07 14:40 | |
| 1 | 2 | add_item_to_cart | 2022-04-07 14:44 | |
| 1 | 2 | checkout | 2022-04-07 14:47 | |
| 1 | 2 | pay | 2022-04-07 14:50 |
So there are multiple event types, they have timestamp (when it happened) and user that is the actor.
Let's say I want to find all users that did add items to cart and did buy on the next day. I would assume the SQL query should be
SELECT DISTINCT
user_id
FROM
user_event
WHERE
(type = 'add_item_to_cart' AND time BETWEEEN '2022-04-07 00:00' AND '2022-04-08 00:00') AND
(type = 'buy' AND time BETWEEEN '2022-04-08 00:00' AND '2022-04-09 00:00')
Now, I understand that the above condition is basically equal to:
WHERE
type = 'add_item_to_cart' AND time BETWEEEN '2022-04-07 00:00' AND '2022-04-08 00:00' AND
type = 'buy' AND time BETWEEEN '2022-04-08 00:00' AND '2022-04-09 00:00'
which will return always empty results because we apply two conditions to column time with date ranges that don't intersect.
So I have 3 questions
- How do I rewrite the query so that I get customers that added item to cart in 1 date range and bought in another date range ?
- The above condition actually would match purchases made almost 2 days from adding to cart, i.e. '2022-04-07 00:01' - added to cart, '2022-04-08 23:58' - bought. How do I make a condition related to previous one, i.e. match buys strictly less than 1 day from last date of checkout (with matching user id) ?
- Is there a way to force that events are related to the same user, so that query doesn't return user who bought item <1 day after someone else (with different
user_id) added an item to cart ?