27. Collapse double-clicks into one click

SQLMediumBoundary trapTie trap

deduplication · lag · time window · window functions

How the Colab notebook works

The notebook has the data, an answer cell and a check() function. You need a Google account; nothing is installed on your machine. Free Colab runs only two or three notebooks at a time, so end the session of a finished problem (Runtime > Disconnect and delete runtime) before opening the next one. "Mark as solved" is remembered in this browser only.

The situation

The Buy button on a slow page gets clicked two or three times by impatient users, and every click is recorded. Downstream counts of "button clicks" are inflated.

The product team has settled on a rule for what counts as an accidental repeat, and needs the click log with the repeats removed.

Input tables

clicks

One row per recorded click.

Column Type Description
click_id BIGINT Unique. A click recorded later has a higher id.
user_id BIGINT The user who clicked.
button STRING buy, like, share or subscribe.
clicked_at TIMESTAMP_NTZ When the click was recorded, to the millisecond.

Requirement

Return the clicks that are not accidental repeats.

  • A click is a repeat if the same user clicked the same button less than 5 seconds before it.
  • "Before it" means the previous click on that button, whether or not that previous click was itself a repeat. Clicks at 0, 4, 8 and 12 seconds are one real click and three repeats, because each is 4 seconds after the one before.
  • A click exactly 5.000 seconds after the previous one is not a repeat.
  • If two clicks on the same button have the same timestamp, the one with the higher click_id is the repeat.
  • Clicks on different buttons never affect each other.

Expected output

One row per click that is kept.

Column Type Description
click_id BIGINT
user_id BIGINT
button STRING
clicked_at TIMESTAMP_NTZ

Solve it in the browser

This editor runs DuckDB inside your browser. Nothing is installed and nothing is sent to a server. DuckDB's SQL is close to Spark SQL but not identical. Spark's TIMESTAMPDIFF does not exist in DuckDB. Adding an interval, as in clicked_at + INTERVAL 5 SECONDS, works in both. The Colab notebook runs real Spark.

query.sql DuckDB, in your browser
Ctrl + Enter runs the query

Your result appears here. Your draft is kept in this browser.

Explanation

Show explanationGives the answer away. Try the problem first.

The approach

Whether a click is a repeat depends on one other row: the previous click by the same user on the same button. That is LAG:

LAG(clicked_at) OVER (
    PARTITION BY user_id, button
    ORDER BY clicked_at, click_id
) AS prev_clicked_at

Then keep a click when there is no previous one, or when it comes at least 5 seconds later:

WHERE prev_clicked_at IS NULL
   OR clicked_at >= prev_clicked_at + INTERVAL 5 SECONDS

Each part of that is one of the rules in the statement:

  • PARTITION BY user_id, button: clicks on different buttons never affect each other.
  • ORDER BY clicked_at, click_id: two clicks can share a timestamp, and click_id decides which is first.
  • >=: exactly 5.000 seconds later is a new click.
  • prev_clicked_at IS NULL: the first click on a button has nothing before it, and without this line the comparison is NULL and the click is dropped.

LAG cannot be used in a WHERE directly, because window functions are computed after the WHERE. Hence the subquery.

Of the 350 recorded clicks, 188 are kept and 162 are repeats.

The rule compares with the previous click, not the previous kept click

Clicks at 0, 4, 8 and 12 seconds: each is 4 seconds after the one before, so the last three are all repeats and one click is kept. That is user 37.

A different rule is possible: "ignore everything for 5 seconds after a click that counted". Under that rule the click at 8 seconds would count again. That rule cannot be written with one LAG, because whether a click counts depends on the outcome for the clicks before it. The statement chose the simpler rule on purpose. If you are ever given the other one, expect a recursive query or a loop.

Common wrong answers

PARTITION BY user_id only. A click on like two seconds after a click on buy is treated as a repeat of it. 3 real clicks are lost (185 rows).

> instead of >=. The two clicks that come exactly 5.000 seconds after the previous one are dropped (186 rows). Boundaries like this are worth one planted row in any test you write.

ORDER BY clicked_at without click_id. User 38 has two like clicks with the same timestamp. With nothing to break the tie, the engine may put either first. The row count is still 188, but on this data Spark keeps the click with the higher id, and another run or another engine may choose differently. An ORDER BY in a window should identify one order.

Rounding the timestamps to whole seconds first. Subtracting unix_timestamp(...) values compares seconds with the milliseconds cut off. A click at 10.9 s and one at 15.1 s are 4.2 seconds apart, but 15 minus 10 is 5, so the repeat is kept. 17 repeats survive (205 rows). Cutting the difference down to whole seconds is fine; cutting each timestamp first is not.

5-second buckets. Grouping by FLOOR(epoch / 5) and keeping one click per bucket splits a burst whenever it crosses a bucket edge: 4.9 s and 5.1 s are in different buckets. 264 rows. Fixed buckets answer a different question.

The first click per user and button. ROW_NUMBER() = 1 keeps one click per button for the whole day and throws away every later real click. 108 rows.

SELECT DISTINCT on user, button and the second. Only removes clicks inside the same clock second. 330 rows.

Edge cases in the data

Case Where Expected
Clicks at 0, 5.000 and 9.999 s user 36 The first two are kept; the third is 4.999 s after the second.
A chain, each 4 s after the last user 37 One click kept out of four.
Two clicks with the same timestamp user 38, like The lower click_id is kept.
A different button one second later user 38, share Kept: it is the first click on that button.
Two buttons interleaved user 39 buy at 0 and at 5.000 s both kept; like at 2.0 s kept, like at 6.999 s dropped.