Carvana's Product Analytics team wants to understand how shoppers move through vehicle detail pages during completed web sessions. Write a PostgreSQL query using LAG and LEAD to expose the previous and next action surrounding each vehicle view.
Requirements
- Include only completed web sessions that started in January 2026.
- Return one row for each
vehicle_view event, including the previous event type, next event type, and seconds until the next event.
- Partition the window calculations by session and order events chronologically, using
event_id as a tie-breaker.
- Preserve
NULL when there is no previous or next event.
Output details
- Return columns in this order:
session_id, vehicle_id, event_ts, previous_event_type, next_event_type, seconds_to_next.
- Sort results by
session_id, then event_ts, then event_id, all ascending.
- Include sessions starting at or after
TIMESTAMP '2026-01-01 00:00:00' and before TIMESTAMP '2026-02-01 00:00:00'.
- Report
seconds_to_next as an integer using PostgreSQL cast semantics for elapsed epoch seconds.