DataEng Hub
SilverSpark SQLAbout 25 minutes · sql

Sessionize clickstream with window functions

Group raw page views into sessions using a 30-minute inactivity gap, with pure SQL window functions.

Scenario

NorthBridge Retail's web analytics vendor charges per event. The team wants sessions computed in-house: a session ends after 30 minutes of inactivity for that user.

Input data

Table: page_views

user_id page ts
U-1 /home 2026-10-05 09:00:00
U-1 /search 2026-10-05 09:10:00
U-1 /product/42 2026-10-05 09:55:00
U-1 /cart 2026-10-05 10:00:00
U-2 /home 2026-10-05 09:05:00
U-2 /product/7 2026-10-05 11:00:00
Setup script
CREATE OR REPLACE TABLE page_views (
  user_id STRING, page STRING, ts TIMESTAMP
);
INSERT INTO page_views VALUES
  ('U-1', '/home',       '2026-10-05 09:00:00'),
  ('U-1', '/search',     '2026-10-05 09:10:00'),
  ('U-1', '/product/42', '2026-10-05 09:55:00'),
  ('U-1', '/cart',       '2026-10-05 10:00:00'),
  ('U-2', '/home',       '2026-10-05 09:05:00'),
  ('U-2', '/product/7',  '2026-10-05 11:00:00');

Task

  1. Assign a session_id to every page view: user_id plus a per-user session number (1, 2, 3…).
  2. A new session starts when the gap from the user's previous event exceeds 30 minutes.

Expected output

user_id page ts session_id
U-1 /home 2026-10-05 09:00:00 U-1-1
U-1 /search 2026-10-05 09:10:00 U-1-1
U-1 /product/42 2026-10-05 09:55:00 U-1-2
U-1 /cart 2026-10-05 10:00:00 U-1-2
U-2 /home 2026-10-05 09:05:00 U-2-1
U-2 /product/7 2026-10-05 11:00:00 U-2-2

Constraints

  • Pure SQL, no procedural loops.
  • The first event of each user starts session 1.

Hints

Hint 1

You need each row to see the previous row's timestamp for the same user. Which window function does that?

Hint 2

Two passes: first flag session starts (gap > 30 min or no previous event), then turn flags into numbers with a cumulative SUM over the same window ordering.

Solution

Show solution
-- tested on: Databricks Runtime 15.x LTS
WITH flagged AS (
  SELECT *,
         CASE
           WHEN LAG(ts) OVER (PARTITION BY user_id ORDER BY ts) IS NULL THEN 1
           WHEN ts - LAG(ts) OVER (PARTITION BY user_id ORDER BY ts)
                > INTERVAL 30 MINUTES THEN 1
           ELSE 0
         END AS is_new_session
  FROM page_views
),
numbered AS (
  SELECT *,
         SUM(is_new_session) OVER (
           PARTITION BY user_id ORDER BY ts
           ROWS UNBOUNDED PRECEDING
         ) AS session_num
  FROM flagged
)
SELECT user_id, page, ts,
       CONCAT(user_id, '-', session_num) AS session_id
FROM numbered
ORDER BY user_id, ts;

Explanation

LAG exposes the previous event's time; a gap larger than the threshold (or no previous event) marks a session start. The running SUM of those 1/0 flags is exactly the session number — flags increment it only at boundaries.

Alternative approaches

  • Databricks/Spark 3.2+: SESSION_WINDOW(ts, '30 minutes') in a GROUP BY aggregates straight to session level — great when you don't need the session id back on each event row.

Performance notes

Both windows share PARTITION BY user_id ORDER BY ts, so Spark evaluates them in one shuffle/sort. Skewed users (bots!) skew the stage — filter known bots before the window.

Follow-up questions

  • How would you also emit a sessions table (start, end, pages, duration) in the same query?
  • What breaks when events arrive hours late, and how does streaming's watermark address it?

Learn the concept

Keep going

ProblemBronze

Deduplicate a raw events table

Keep exactly one row per event_id — the latest by timestamp — from a bronze table that receives replays and retries.