Daily New, Retained and Churned Devices by Platform
SQL & DataModel answerLast updated
Write one SQL query that classifies devices as new, retained or churned for 2025-09-01 from a raw event log, and reports net growth overall and per platform.
Daily device growth report
You have a raw event table from a consumer app. Each row is one event emitted by a device.
events(device_id STRING, event_name STRING, ts TIMESTAMP, device_platform STRING)
Build a report for the reporting day 2025-09-01, with 2025-08-31 as the previous day. All dates are calendar dates in UTC. A device is "active" on a day if it has at least one event that day.
Definitions
- New: a device whose earliest event, across all history, falls on 2025-09-01.
- Retained: a device active on both 2025-08-31 and 2025-09-01.
- Churned: a device active on 2025-08-31 but not on 2025-09-01.
- Net:
new + retained - churned.
Output
One row for the whole population and one row per device_platform, with columns:
event_date, device_platform, new_users, retained_users, churn_users, net_users
Use standard SQL with CTEs. Window functions are not required. Count each device once per bucket, even if it has many events.
Sample data (UTC)
| device_id | event_name | ts | device_platform |
|---|---|---|---|
| u1 | app_open | 2025-08-31T23:50:00Z | applemobile |
| u1 | click | 2025-09-01T01:00:00Z | applemobile |
| u2 | app_open | 2025-08-31T15:00:00Z | appletv |
| u2 | purchase | 2025-09-02T03:00:00Z | appletv |
| u3 | app_open | 2025-09-01T10:00:00Z | desktop |
| u4 | app_open | 2025-08-31T18:00:00Z | desktop |
| u5 | app_open | 2025-09-01T16:00:00Z | applemobile |
| u6 | app_open | 2025-08-30T12:00:00Z | applemobile |
| u6 | app_open | 2025-08-31T12:00:00Z | applemobile |
What to deliver
- State the assumptions you need. At minimum, decide how to handle:
- events after the reporting day,
- a device that changes platform between the two days,
- devices that return after a gap (active today, not yesterday, not new).
- Write a single query that produces the report.
- Show the result you expect on the sample and explain why it is correct.
Hints
Hint 1
Collapse the log to one row per device before counting anything. Once each device appears once, every bucket is a simple yes/no condition.
Hint 2
You need three facts per device: its first-ever active date (over all history), whether it was active on 08-31, and whether it was active on 09-01. A FULL OUTER JOIN between the set of devices active today and the set active yesterday exposes churners, who exist only on the yesterday side.
Hint 3
Decide on one platform label per device so the platform rows add up to the overall row. A reasonable rule is the platform of the latest event today if the device is active today, otherwise the latest event yesterday. Find the latest event with MAX(ts) and join back, instead of a window function. Also remember to exclude events after 2025-09-01.
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Assumptions to state first
- UTC dates. Derive
event_dateasCAST(ts AS DATE)with the session in UTC. On engines with time zone aware types, convert explicitly. This matters foru1: its 23:50 event on 08-31 belongs to 08-31, and only its 01:00 event counts for 09-01. - Ignore the future. Keep only
ts < '2025-09-02'. Otherwiseu2's purchase on 09-02 could be mistaken for activity or distort first-seen dates in a re-run. - "First-ever" uses full history.
u6first appears on 08-30, so the first-seen calculation must not be limited to the last two days. - One platform per device. Status is decided per device, not per (device, platform). The label is the platform of the device's latest event today if it is active today, otherwise its latest event yesterday. Ties on the same timestamp break with
MAX(device_platform). A device that moves from appletv to desktop is therefore retained on desktop, not churn on appletv plus something else. This keeps per-platform rows summing exactly to the overall row. - Returning users are in no bucket. A device active today, inactive yesterday and not new matches none of the three definitions, so it is excluded. A real dashboard would normally add a "resurrected" bucket, so mention this.
- Overall row is labeled
device_platform = 'ALL'.
Approach
Reduce the log to one row per relevant device:
first_date: the minimum event date across all history.active_todayandactive_yday: 0/1 flags.device_platform: the single attributed label.
Then:
- New:
first_date = 2025-09-01(this automatically means not active yesterday). - Retained:
active_yday = 1 AND active_today = 1. - Churned:
active_yday = 1 AND active_today = 0.
Because each device appears once, summing the 0/1 flags is the same as a distinct count. The buckets are mutually exclusive, so new + retained - churn does not double count anyone.
Query
WITH events_utc AS (
SELECT device_id,
device_platform,
ts,
CAST(ts AS DATE) AS d
FROM events
WHERE ts < TIMESTAMP '2025-09-02 00:00:00'
),
first_day AS (
SELECT device_id, MIN(d) AS first_date
FROM events_utc
GROUP BY device_id
),
latest_per_day AS (
SELECT device_id, d, MAX(ts) AS last_ts
FROM events_utc
WHERE d IN (DATE '2025-08-31', DATE '2025-09-01')
GROUP BY device_id, d
),
platform_per_day AS (
SELECT l.device_id,
l.d,
MAX(e.device_platform) AS device_platform
FROM latest_per_day l
JOIN events_utc e
ON e.device_id = l.device_id
AND e.ts = l.last_ts
GROUP BY l.device_id, l.d
),
today AS (
SELECT device_id, device_platform
FROM platform_per_day
WHERE d = DATE '2025-09-01'
),
yesterday AS (
SELECT device_id, device_platform
FROM platform_per_day
WHERE d = DATE '2025-08-31'
),
status AS (
SELECT COALESCE(t.device_id, y.device_id) AS device_id,
COALESCE(t.device_platform, y.device_platform) AS device_platform,
CASE WHEN t.device_id IS NOT NULL THEN 1 ELSE 0 END AS active_today,
CASE WHEN y.device_id IS NOT NULL THEN 1 ELSE 0 END AS active_yday
FROM today t
FULL OUTER JOIN yesterday y
ON t.device_id = y.device_id
),
flags AS (
SELECT s.device_platform,
CASE WHEN f.first_date = DATE '2025-09-01' THEN 1 ELSE 0 END AS is_new,
CASE WHEN s.active_today = 1 AND s.active_yday = 1 THEN 1 ELSE 0 END AS is_retained,
CASE WHEN s.active_today = 0 AND s.active_yday = 1 THEN 1 ELSE 0 END AS is_churn
FROM status s
JOIN first_day f
ON f.device_id = s.device_id
),
totals AS (
SELECT device_platform,
SUM(is_new) AS new_users,
SUM(is_retained) AS retained_users,
SUM(is_churn) AS churn_users
FROM flags
GROUP BY device_platform
UNION ALL
SELECT 'ALL', SUM(is_new), SUM(is_retained), SUM(is_churn)
FROM flags
)
SELECT DATE '2025-09-01' AS event_date,
device_platform,
new_users,
retained_users,
churn_users,
new_users + retained_users - churn_users AS net_users
FROM totals
ORDER BY CASE WHEN device_platform = 'ALL' THEN 0 ELSE 1 END, device_platform;
Why it works
- FULL OUTER JOIN keeps churners, who appear only in the yesterday set. An inner or left join from today would silently drop them.
- Latest event via
MAX(ts)plus a join back satisfies the "no window functions" constraint. The outerMAX(device_platform)makes the result deterministic when two events share a timestamp. - Joining to
first_daygives the full-history first date without limiting the scan window for that calculation. ROLLUPcan replace theUNION ALLon engines that support it. Coalesce the NULL key to'ALL'.
Check against the sample
- Active on 08-31: u1, u2, u4, u6.
- Active on 09-01: u1, u3, u5.
- First seen on 09-01: u3, u5. (u2's 09-02 event is filtered out.)
| event_date | device_platform | new | retained | churn | net |
|---|---|---|---|---|---|
| 2025-09-01 | ALL | 2 | 1 | 3 | 0 |
| 2025-09-01 | applemobile | 1 (u5) | 1 (u1) | 1 (u6) | 1 |
| 2025-09-01 | appletv | 0 | 0 | 1 (u2) | -1 |
| 2025-09-01 | desktop | 1 (u3) | 0 | 1 (u4) | 0 |
The platform rows sum to the ALL row for each column, which confirms the one-label-per-device rule.
Complexity and practical notes
- One scan of the filtered events for each of the first-seen and latest-event aggregates, plus a join back on a small set (devices active on two days). Roughly O(N) with hash aggregation. An index or partition on
tsor date cuts the scan cost. - The first-seen step is the expensive one because it needs full history. In production, maintain a
device_first_seentable incrementally instead of rescanning. - Put the two dates in a parameter CTE or bind variables rather than hard-coding them.
Common mistakes
- Using the timestamp instead of the date, so one device is counted several times.
- Computing first-seen only over the last two days, which would mislabel a device that first appeared long ago and went quiet.
- Counting events instead of distinct devices.
- Using an inner join and losing churners.
- Counting by (device, platform) pairs, so a platform switcher shows as both churn and new/retained, and the platform rows no longer reconcile with the total.
- Forgetting to cut off events after the reporting day.
- Not saying out loud that returning devices fall outside the three buckets.