KatInterview

Point-in-Time User Features and 30-Day Purchase Label in SQL

SQL & DataModel answer
MediumModel answerSQL & Datasqlfeature-engineeringpoint-in-time

Last updated

Build a leakage-free training table at a fixed snapshot: deduplicated, arrival-aware 7-day activity features per user, plus a forward-looking 30-day purchase label.

You are preparing a training set for a purchase-prediction model. The set must be built as of a fixed moment, so that every feature reflects only what the system could have known then, and the label looks only forward.

Take snapshot_ts = '2025-09-01 00:00:00' (UTC). The schema is:

users(u_id INT, signup_dt DATE, country STRING)
events(u_id INT, event_time TIMESTAMP, arrival_time TIMESTAMP, event_type STRING, session_id STRING)
orders(order_id INT, u_id INT, order_time TIMESTAMP, amount DECIMAL(10,2))

event_time is when the action happened. arrival_time is when the row landed in the warehouse.

users

u_idsignup_dtcountry
12025-07-20US
22025-08-25CA
32025-08-30US

events

u_idevent_timearrival_timeevent_typesession_idnote
12025-08-29 10:00:002025-08-29 10:00:02viewa
12025-08-31 09:00:002025-08-31 09:00:03add_to_carta
12025-09-01 12:00:002025-09-01 12:00:01viewb
22025-08-30 15:00:002025-08-30 15:00:02viewc
22025-09-01 09:30:002025-09-01 09:30:01viewd
32025-08-31 23:59:002025-09-02 00:00:05viewelate-arriving
32025-09-01 08:00:002025-09-01 08:00:00viewe
32025-09-01 08:00:002025-09-01 08:00:00vieweexact duplicate

orders

order_idu_idorder_timeamount
10112025-09-10 13:00:00120.00
10222025-08-31 16:00:0050.00
10322025-09-03 11:00:0060.00

Task. Write a single SQL query returning one row per user with these columns:

u_id, country, days_since_signup, last_event_time, events_7d, sessions_7d, added_to_cart_7d, y_30d

Rules:

  1. Visibility and dedup. Features may use only events with event_time < snapshot_ts AND arrival_time <= snapshot_ts. Drop duplicates on (u_id, event_time, event_type, session_id).
  2. Window. The 7-day window is [snapshot_ts - 7 days, snapshot_ts).
  3. Counts. sessions_7d is the number of distinct session_id values in the window. events_7d is the number of distinct (event_time, event_type, session_id) combinations in the window.
  4. Label. y_30d = 1 if the user has at least one order in [snapshot_ts, snapshot_ts + 30 days), otherwise 0.
  5. Users without events. They must still appear, with zeros or NULLs filled in sensibly.

Also:

  • Show the exact output rows for the sample data.
  • Explain why user 3's late-arriving 2025-08-31 event must not contribute to the features.

Hints

Hint 1

Start from the user table as the driving set and LEFT JOIN pre-aggregated feature and label subqueries, so users with no activity are not lost and joins do not fan out.

Hint 2

Apply the visibility filter (event_time < snapshot and arrival_time <= snapshot) and de-duplicate first. Then compute last_event_time over all visible rows, and the 7-day counts with conditional aggregation restricted to the window.

Hint 3

Reduce orders to one row per user (the label window is forward-looking and half-open) before joining. Use COALESCE for counts and the label, but leave last_event_time as NULL when nothing is visible.