Compute Sessionization From Raw Events
Groups raw event logs into sessions using an inactivity gap and derives per-session metrics in SQL.
Prompt
ROLE: You are a data engineer who sessionizes clickstream/event data. CONTEXT: Sessionize events in [EVENT_TABLE] (columns: user_id, event_timestamp, ...). A new session starts after [GAP] minutes of inactivity for the same user. Engine: [DATABASE_ENGINE]. TASK: 1. Explain the gap-based sessionization algorithm: order events per user, compute the gap to the previous event with LAG, mark a new-session boundary when the gap exceeds the threshold, then cumulatively sum the boundary flags to assign a session number. 2. Write the SQL to produce a session_id per event (user_id + session sequence, or a hashed/uuid surrogate). 3. Roll up to one row per session with: start time, end time, duration, event count, distinct pages/screens, and entry and exit events. 4. Handle edge cases: single-event sessions (duration 0), users with one event, and timezone of timestamps. 5. Show how to compute average session duration and sessions-per-user from the output. OUTPUT FORMAT: Algorithm explanation -> Event-level ```sql``` (session assignment) -> Session-level rollup ```sql``` -> Edge-case handling -> Example aggregates. CONSTRAINTS: Partition by user and order by timestamp before LAG/SUM. Make [GAP] a parameter. Define duration boundaries clearly. Do not let events from different users share a session.
How to use this prompt
- 1
Copy the prompt above and paste it into ChatGPT, Claude, or Gemini — or open it in the visual Studio to edit each part on a canvas and run it with your own key.
- 2
Replace any bracketed placeholders with your specifics. The more concrete your context and constraints, the sharper the result — see the 5-part prompt structure.
- 3
Run it, then refine. Ask the model to critique and improve its own answer with self-critique prompting.
Techniques in this prompt
Assigns the model an expert persona so it adopts the right vocabulary, depth, and standards for the task.
Learn this techniqueAsks the model to reason step by step before answering — ideal for multi-step, logical, or analytical tasks.
Learn this techniqueForces explicit intermediate reasoning instead of jumping to a conclusion, which improves accuracy on hard tasks.
Learn this techniqueRecommended models
Build on this prompt
Open it in the visual Studio to wire it into a full workflow with your own API key — or learn the craft behind prompts like this.
More in Data Analysis & SQL
Translate Business Questions Into SQL
Turns a plain-English stakeholder question into a correct, well-commented SQL query against a known schema.
Optimize A Slow SQL Query
Diagnoses why a query is slow and rewrites it with targeted, explained optimizations and an index plan.
Debug A SQL Query That Returns Wrong Results
Systematically finds the logic error producing incorrect numbers and delivers a corrected, verified query.
Explain An Unfamiliar SQL Query In Plain English
Reverse-engineers a complex inherited query into a clear narrative, business meaning, and risk list.