The situation
The app shows every user a badge for their longest login streak: the most days in a row on which they opened the app.
The badge was wrong for the most active users. People who logged in several times a day were shown streaks of one or two days, although they had not missed a day in weeks.
Input tables
app_logins
One row per login. A user can log in several times on the same day.
| Column | Type | Description |
|---|---|---|
user_id |
BIGINT | The user. |
login_date |
DATE | The calendar date of the login. |
Requirement
Return each user's longest streak.
- A streak is a run of consecutive calendar days on each of which the user logged in at least once. 31 December followed by 1 January is consecutive.
- Several logins on one day count as one day.
streak_daysis the number of days in the streak. A user who logged in on a single day has a streak of 1.- If a user has two or more streaks of the same longest length, return the most recent of them.
Expected output
Exactly one row per user.
| Column | Type | Description |
|---|---|---|
user_id |
BIGINT | |
streak_start |
DATE | First day of the streak. |
streak_end |
DATE | Last day of the streak. |
streak_days |
BIGINT | Number of days from start to end, both included. |
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. DATE_SUB(date, n) and the two-argument DATEDIFF are Spark only. DATE_ADD(login_date, CAST(-n AS INT)) works in both engines.
The Colab notebook runs real Spark.
Your result appears here. Your draft is kept in this browser.
Explanation
Show explanationGives the answer away. Try the problem first.
The approach
This is the classic "gaps and islands" problem: find the islands (runs of consecutive days), then pick the longest.
Step 1: one row per user and day.
SELECT DISTINCT user_id, login_date FROM app_logins
A streak is counted in days. Three logins on one day are one day. Everything after this step assumes one row per day, so it has to come first.
Step 2: find a value that is the same for every day of a run. Number each user's
days in date order with ROW_NUMBER(). Inside a run of consecutive days the date
goes up by one and the number goes up by one, so the difference between them does
not change:
| login_date | day_number | date minus number |
|---|---|---|
| 2025-01-03 | 1 | 2025-01-02 |
| 2025-01-04 | 2 | 2025-01-02 |
| 2025-01-05 | 3 | 2025-01-02 |
| 2025-01-09 | 4 | 2025-01-05 |
| 2025-01-10 | 5 | 2025-01-05 |
After a gap the date jumps and the number does not, so the difference changes. The date in the third column means nothing by itself. It is only a label for the run.
Step 3: one row per run. Group by user and that label: MIN(login_date) is the
start, MAX(login_date) the end, COUNT(*) the length.
Step 4: the longest run per user. ROW_NUMBER() ordered by
streak_days DESC, streak_end DESC, keep position 1. The second sort key is the
tie rule from the statement: of two equally long streaks, the more recent.
Because the arithmetic is on dates, month ends and New Year need no special handling. 31 December plus one day is 1 January.
There are 498 logins by 27 users on 370 different user-days.
Common wrong answers
Skipping step 1. With several rows for one day, the row numbers keep going up while the date stands still, and the "date minus number" label changes inside a run. The run is cut into pieces. This is the bug in the story: the most active users, who log in several times a day, are the ones who lose their streak. 23 of the 27 users have at least one day with two or more logins, and the result is wrong for 22 of them. User 33 logged in three times on 31 December and gets a streak of 4 days instead of 6.
DENSE_RANK() on the raw logins, then COUNT(*). DENSE_RANK gives all rows
of one day the same number, so the label is right and the runs are found correctly.
But COUNT(*) now counts logins, not days. Wrong for 19 users. With
COUNT(DISTINCT login_date) this approach is correct.
No rule for ties. ORDER BY streak_days DESC alone leaves the choice between
two equal streaks to the engine. Five users have such a tie. Sorting by
streak_end ASC returns the oldest of the streaks instead of the most recent and
is wrong for all five.
RANK() instead of ROW_NUMBER(). RANK gives tied streaks the same position
and returns both. 36 rows for 27 users.
Counting consecutive pairs. Flag every day whose previous day is also a login day, sum the flags and add one. That counts the consecutive days of all streaks together. A user with streaks of 4 and 4 gets 7. Wrong for 25 users.
First login to last login. DATEDIFF(MAX, MIN) + 1 is the span of the user's
activity, gaps included. Wrong for 25 users.
streak_days as an INT. DATEDIFF(streak_end, streak_start) + 1 gives the
right numbers, but as INT. The output column is BIGINT, which is what COUNT(*)
returns. Cast it, or count.
The other standard method
Use LAG to flag the first day of every run (the previous day is not yesterday),
then take a running sum of the flags. The running sum is a run number. It is one
step longer and reads more naturally to some people; the result is the same.
Edge cases in the data
| Case | Where | Expected |
|---|---|---|
| A single login | user 31 | A streak of 1 day. |
| Two streaks of 4 days | user 32 | The later one, 10 to 13 February. |
| Across New Year, three logins on 31 December | user 33 | 29 December to 3 January, 6 days. |
| Across the end of February | user 34 | 27 February to 2 March, 4 days. 2025 is not a leap year. |
| Every other day | user 35 | Five streaks of 1 day; the most recent is 10 January. |