Events, then sessions
So far, one row meant one order. Now one row means one event. An event is one action by a customer at one time, such as a page view or a purchase.
Teams rarely ask about single events. They ask about groups of events. In this chapter you build three kinds of group:
- A session: events from one customer that are close in time. It is one visit.
- A streak: days in a row on which a customer was active.
- A funnel: steps in a fixed order, with a count of customers at each step.
None of these is a column in a table. You build each one from the event rows.
You need window functions from chapter 5. A window function looks at nearby rows without merging them.
This chapter uses LAG, ROW_NUMBER, SUM() OVER and PARTITION BY. If you forget one, each is explained again where it is used.
The events table
This chapter adds one table, events. Each row is one action: who did it, when, and what. Omar (customer 5) has no events, as he had no orders.
| customer_id | event_time | event_name |
|---|---|---|
| 1 | 2026-01-03 09:00:00 | view |
| 1 | 2026-01-03 09:10:00 | cart |
| 1 | 2026-01-03 09:20:00 | purchase |
| 1 | 2026-01-03 14:00:00 | view |
| 1 | 2026-01-04 10:00:00 | view |
| 1 | 2026-01-05 10:00:00 | view |
| 1 | 2026-01-08 10:00:00 | view |
| 2 | 2026-01-03 11:00:00 | view |
| 2 | 2026-01-03 11:30:00 | cart |
| 3 | 2026-01-03 16:00:00 | view |
| 4 | 2026-01-04 10:00:00 | view |
SELECT customer_id, event_time, event_name
FROM events
ORDER BY customer_id, event_time;Follow Asha, customer 1. She has seven events on four different days. She is the best customer to check by hand.
