topskill.dev
CoursesTracks
0
All courses/Data Analysis Using SQLStage 4: compare cohort and channel denominators
Game churn: a fixed-window player investigation
5 / 8
← Data Analysis Using SQL
○Define the row before the metric○Exercise: define the row before the metric○Profile missing values without hiding them○Exercise: profile missing values without hiding them○Group records by a business key○Exercise: Group records by a business key○Filter completed groups with HAVING○Exercise: Filter completed groups with HAVING✦Module Quiz
○Remove surrounding spaces with TRIM○Exercise: Remove surrounding spaces with TRIM○Normalize case with LOWER○Exercise: Normalize case with LOWER○Mark a blank as missing with NULLIF○Exercise: Mark a blank as missing with NULLIF○Choose a display fallback with COALESCE○Exercise: Choose a display fallback with COALESCE○Test decimal text with a regular expression○Exercise: Test decimal text with a regular expression○Choose a result value with CASE○Exercise: Choose a result value with CASE○Convert approved text to numeric○Exercise: Convert approved text to numeric✦Module Quiz
○Sum a measure at the chosen grain○Exercise: Sum a measure at the chosen grain○Calculate the mean of known observations○Exercise: Calculate the mean of known observations○Find the smallest known observation○Exercise: Find the smallest known observation○Find the largest known observation○Exercise: Find the largest known observation○Round a numeric report value○Exercise: Round a numeric report value✦Module Quiz
○Count matching rows with FILTER○Exercise: Count matching rows with FILTER○Define both sides of a rate○Exercise: Define both sides of a rate○Compare a row with a computed benchmark○Exercise: compare a row with a computed benchmark○Name analysis steps with CTEs○Exercise: name analysis steps with CTEs○Publish a query definition as a view○Exercise: Publish a query definition as a view○Commit a reporting change as one transaction○Exercise: Commit a reporting change as one transaction○Intake project 1: a reviewable clean layer○Intake project 2: region coverage○Intake project 3: one defensible coverage rate✦Module Quiz
○Make unmatched records visible○Exercise: make unmatched records visible○Prevent join fanout with preaggregation○Exercise: prevent join fanout with preaggregation○Use existence when you do not need child rows○Exercise: use existence when you do not need child rows○Refund project 1: one row per refunded order○Refund project 2: preserve the paid-order population○Refund project 3: reconcile the regional ledger✦Module Quiz
○Interpret an instant in a reporting zone○Exercise: Interpret an instant in a reporting zone○Keep adjacent time periods separate○Exercise: Keep adjacent time periods separate○Bucket calendar months without losing the year○Exercise: bucket calendar months without losing the year○Advance a calendar boundary with INTERVAL○Exercise: Advance a calendar boundary with INTERVAL○Read a date component with EXTRACT○Exercise: Read a date component with EXTRACT○Fill calendar gaps before measuring trends○Exercise: fill calendar gaps before measuring trends✦Module Quiz
○Keep detail rows while computing group shares○Exercise: keep detail rows while computing group shares○Assign a unique position with ROW_NUMBER○Exercise: Assign a unique position with ROW_NUMBER○Share tied positions with RANK○Exercise: Share tied positions with RANK○Number distinct value levels with DENSE_RANK○Exercise: Number distinct value levels with DENSE_RANK○Retain one source version per event○Exercise: Retain one source version per event✦Module Quiz
○Look back one observation with LAG○Exercise: Look back one observation with LAG○Look ahead one observation with LEAD○Exercise: Look ahead one observation with LEAD○Accumulate a running total with an explicit frame○Exercise: Accumulate a running total with an explicit frame○Restrict a total to a trailing row window○Exercise: Restrict a total to a trailing row window○Interpolate a percentile with PERCENTILE_CONT○Exercise: Interpolate a percentile with PERCENTILE_CONT○Choose an observed percentile with PERCENTILE_DISC○Exercise: Choose an observed percentile with PERCENTILE_DISC○Daily briefing 1: a complete daily layer○Daily briefing 2: cumulative and trailing totals○Daily briefing 3: changes with honest baselines○Rank a review queue without erasing ties○Exercise: rank a review queue without erasing ties✦Module Quiz
○Build a cohort from the first eligible purchase○Exercise: build a cohort from the first eligible purchase○Reduce repeated activity to one row per combination○Exercise: Reduce repeated activity to one row per combination○Count distinct known identities○Exercise: Count distinct known identities○Separate zero retention from an unobserved period○Exercise: separate zero retention from an unobserved period○Require the funnel steps to happen in sequence○Exercise: require the funnel steps to happen in sequence○Measure recency at a fixed cutoff○Exercise: Measure recency at a fixed cutoff○Measure purchase frequency at order grain○Exercise: Measure purchase frequency at order grain○State the monetary measure in a member report○Exercise: State the monetary measure in a member report○Apply a segment policy to prepared RFM measures○Exercise: Apply a segment policy to prepared RFM measures✦Module Quiz
○Create an empty reporting table○Exercise: Create an empty reporting table○Protect report row identity with a primary key○Exercise: Protect report row identity with a primary key○Protect a reporting relationship with REFERENCES○Exercise: Protect a reporting relationship with REFERENCES○Load query results with INSERT ... SELECT○Exercise: Load query results with INSERT ... SELECT○Model a report with facts and dimensions○Exercise: model a report with facts and dimensions○Reconcile independent sources and name the limits○Exercise: reconcile independent sources and name the limits○Inspect the proposed query plan○Exercise: Inspect the proposed query plan○Declare an index for a filter column○Exercise: Declare an index for a filter column✦Module Quiz
○Quarter review 1: freeze the report population○Quarter review 2: apply the refund cutoff once○Quarter review 3: regional evidence with an unmatched bucket○Quarter review 4: a monthly trend with a stable definition○Quarter review 5: publish a reconciled evidence table✦Module Quiz
○Define activity churn before counting players○Stage 1: establish the observed play stream○Stage 2: preserve each player and last-seen history○Stage 3: classify inactivity without losing exclusions●Stage 4: compare cohort and channel denominators○Stage 5: reconcile eligibility and boundary cases○Stage 6: prioritize investigation with honest evidence✦Module Quiz

Stage 4: compare cohort and channel denominators

Data model

Exercise data

These objects are loaded at the start. Only the query inputs and their required lineage are listed; other tables in a shared seed are not part of this report.

game_activity

One row represents one valid matched logical play.

Field / SQL typeMeaningRules
event_id · textRetained logical event identity of a completed puzzle attempt.Non-NULL by query lineage, not a declared NOT NULL constraint. Logical unique event key inherited from game_observed; not a physical primary key.
player_id · integerMatched account identity for this play.Non-NULL by query lineage, not a declared NOT NULL constraint. Equality with the account primary key excludes missing and orphan references; many plays can match one account.
happened_at · timestamp with time zonePlay occurrence instant in UTC, on or after account signup and before the fixed cutoff.Non-NULL by query lineage, not a declared NOT NULL constraint.

This ordinary view stores a query definition, not frozen rows; it has no independently declared primary key, foreign key or NOT NULL constraint.

The inner join adds validity, not another version choice. Heartbeats do not count as play; receipt time determined knowledge upstream, while occurrence time determines activity windows.

game_events

One row represents one received version of a Lantern Trail event.

Field / SQL typeMeaningRules
ingest_id · integerUnique receipt identifier, also the tie-breaker for equal receipt timestamps.Primary key; unique. Required; cannot be NULL.
event_id · textLogical event identity; repeated IDs represent versions or deliveries, not additional plays.Required; cannot be NULL.
player_id · integerLogical account reference; no foreign key excludes orphan or missing player IDs.Nullable; NULL means no account reference is recorded.
kind · textEvent type; play means a completed puzzle attempt, while heartbeat is not play.Required; cannot be NULL.
happened_at · timestamp with time zoneEvent occurrence instant in UTC; used for play sequence, signup validity and activity windows.Required; cannot be NULL.
received_at · timestamp with time zoneArrival instant in UTC; used to restrict knowledge and rank known event versions.Required; cannot be NULL.

Kind labels are not CHECK-enforced. Restrict both occurrence and receipt to before the fixed cutoff, then retain the latest remaining version per event_id. Valid activity also needs a matching account and occurrence on or after signup. A logical-event ID has no physical UNIQUE constraint.

game_observed

One row represents one latest known logical event before the fixed cutoff.

Field / SQL typeMeaningRules
event_id · textLogical event identity after retaining one known version.Non-NULL by query lineage, not a declared NOT NULL constraint. Logically unique from the version rule; no physical UNIQUE constraint on this view.
player_id · integerNullable logical account reference; an orphan can survive this version layer.May be NULL under its source or expression policy.
kind · textEvent type from the retained version, including non-play events.Non-NULL by query lineage, not a declared NOT NULL constraint.
happened_at · timestamp with time zoneRetained event occurrence instant in UTC before April 1, 2025.Non-NULL by query lineage, not a declared NOT NULL constraint.
received_at · timestamp with time zoneRetained event receipt instant in UTC before April 1, 2025.Non-NULL by query lineage, not a declared NOT NULL constraint.

This ordinary view stores a query definition, not frozen rows; it has no independently declared primary key, foreign key or NOT NULL constraint.

Apply occurrence and receipt cutoffs before choosing the latest remaining received version. This layer still contains orphans, non-plays and pre-signup events; it is not the valid play stream.

game_player_history

One row represents one account registered before the fixed cutoff.

Field / SQL typeMeaningRules
player_id · integerAccount identity inherited from game_players.player_id.Non-NULL by query lineage, not a declared NOT NULL constraint. Logical unique non-NULL account key; the view has no separately declared primary key.
signed_up_at · timestamp with time zoneAccount signup instant inherited from the account directory, interpreted in UTC.Non-NULL by query lineage, not a declared NOT NULL constraint.
channel · textAcquisition-channel label inherited from the account directory; NULL means unknown attribution.May be NULL under its source or expression policy.
last_seen · timestamp with time zoneLatest known valid play occurrence instant in UTC across all prior valid history.May be NULL under its source or expression policy. NULL means no valid play was observed; it is not signup time or a latest-arrival time.
baseline_plays · bigintValid plays during March 4 inclusive to March 18 exclusive, 2025 UTC.Non-NULL by query lineage, not a declared NOT NULL constraint. COUNT returns zero for no matching baseline plays; not a count of receipts.
recent_plays · bigintValid plays during March 18 inclusive to April 1 exclusive, 2025 UTC.Non-NULL by query lineage, not a declared NOT NULL constraint. COUNT returns zero for no matching follow-up plays.
account_mature · integerComputed integer flag: 1 for signup at or before March 18, 2025 00:00 UTC, else 0.Non-NULL by query lineage, not a declared NOT NULL constraint. A maturity flag alone does not establish positive baseline activity; no CHECK constraint is created.

This ordinary view stores a query definition, not frozen rows; it has no independently declared primary key, foreign key or NOT NULL constraint.

A left join preserves accounts with no valid play. The logical account key remains unique after aggregation; never-played history stays unknown rather than zero time.

game_players

One row represents one Lantern Trail account.

Field / SQL typeMeaningRules
player_id · integerUnique account identifier, not an event or receipt identifier.Primary key; unique. Required; cannot be NULL.
signed_up_at · timestamp with time zoneAccount signup instant; UTC defines signup-month and maturity boundaries.Required; cannot be NULL.
channel · textRecorded acquisition channel; NULL means attribution is unknown, not a new channel called unknown.Nullable; NULL means acquisition attribution is unknown.

Channels are descriptive sample labels, not an enforced vocabulary or a causal explanation of inactivity.

game_status

One row represents one observed account with its activity classification.

Field / SQL typeMeaningRules
player_id · integerAccount identity inherited from game_players.player_id.Non-NULL by query lineage, not a declared NOT NULL constraint. Logical unique non-NULL account key; the view has no separately declared primary key.
signed_up_at · timestamp with time zoneAccount signup instant inherited from the account directory, interpreted in UTC.Non-NULL by query lineage, not a declared NOT NULL constraint.
channel · textAcquisition-channel label inherited from the account directory; NULL means unknown attribution.May be NULL under its source or expression policy.
last_seen · timestamp with time zoneLatest known valid play occurrence instant in UTC across all prior valid history.May be NULL under its source or expression policy. NULL means no valid play was observed; it is not signup time or a latest-arrival time.
baseline_plays · bigintValid plays during March 4 inclusive to March 18 exclusive, 2025 UTC.Non-NULL by query lineage, not a declared NOT NULL constraint. COUNT returns zero for no matching baseline plays; not a count of receipts.
recent_plays · bigintValid plays during March 18 inclusive to April 1 exclusive, 2025 UTC.Non-NULL by query lineage, not a declared NOT NULL constraint. COUNT returns zero for no matching follow-up plays.
account_mature · integerComputed integer flag: 1 for signup at or before March 18, 2025 00:00 UTC, else 0.Non-NULL by query lineage, not a declared NOT NULL constraint. A maturity flag alone does not establish positive baseline activity; no CHECK constraint is created.
activity_status · textReporting label: new_unobserved, never_played, outside_baseline, inactive or retained.Non-NULL by query lineage, not a declared NOT NULL constraint. Computed exhaustive CASE label, not an enforced status vocabulary. Only inactive and retained enter the common eligible denominator.

This ordinary view stores a query definition, not frozen rows; it has no independently declared primary key, foreign key or NOT NULL constraint.

All history fields pass through unchanged. Inactive means no follow-up activity in this fixed report, not cancellation or permanent loss.

Tables and views to define

These objects do not exist in the starting database. Their fields describe the target definition, not additional columns already available to the starter.

game_cohorts

One row represents one eligible signup-month/channel cell.

Field / SQL typeMeaningRules
signup_month · dateUTC first-of-month signup date for these accounts, not a first-play acquisition month.Non-NULL by query lineage, not a declared NOT NULL constraint. The supplied session time zone is UTC; part of the logical composite group key.
channel · textAcquisition-channel group, with missing attribution displayed as unknown.Non-NULL by query lineage, not a declared NOT NULL constraint. Other half of the logical composite key; not a physical UNIQUE constraint.
eligible_players · bigintAccounts classified inactive or retained in this same cell.Non-NULL by query lineage, not a declared NOT NULL constraint. Non-NULL COUNT; excludes new, never-played and outside-baseline accounts.
inactive_players · bigintEligible accounts in this cell with zero valid follow-up plays.Non-NULL by query lineage, not a declared NOT NULL constraint. Non-NULL filtered COUNT; a count of players, not plays.
retained_players · bigintEligible accounts in this cell with at least one valid follow-up play.Non-NULL by query lineage, not a declared NOT NULL constraint. Non-NULL filtered COUNT; complementary to inactive for this denominator.
inactivity_pct · numericInactive accounts as a percentage of eligible accounts in this same cell.Non-NULL by query lineage, not a declared NOT NULL constraint. Computed numeric percentage rounded to two decimals; groups are nonempty, so the guarded denominator is positive.

This ordinary view stores a query definition, not frozen rows; it has no independently declared primary key, foreign key or NOT NULL constraint.

These are descriptive signup strata of a fixed baseline population, not age-standardized retention curves. A small cell can produce an extreme rate.

Named intermediate results

These dictionaries describe the columns exposed by the named query steps. They are not extra preloaded tables or a complete answer query.

versions

One row represents one cutoff-eligible received event version with a version position.

Field / SQL typeMeaningRules
ingest_id · integerUnique receipt identifier, also the tie-breaker for equal receipt timestamps.Non-NULL by query lineage, not a declared NOT NULL constraint. Inherited source value; this step ranks receipts without removing older versions.
event_id · textRequired logical event identity inherited from game_events; it can repeat across these cutoff-eligible versions.Non-NULL by query lineage, not a declared NOT NULL constraint. Not unique at received-version grain; ranking alone does not remove receipts.
player_id · integerLogical account reference; no foreign key excludes orphan or missing player IDs.May be NULL under its source or expression policy. Inherited source value; this step ranks receipts without removing older versions.
kind · textEvent type; play means a completed puzzle attempt, while heartbeat is not play.Non-NULL by query lineage, not a declared NOT NULL constraint. Inherited source value; this step ranks receipts without removing older versions.
happened_at · timestamp with time zoneEvent occurrence instant in UTC; used for play sequence, signup validity and activity windows.Non-NULL by query lineage, not a declared NOT NULL constraint. Inherited source value; this step ranks receipts without removing older versions.
received_at · timestamp with time zoneArrival instant in UTC; used to restrict knowledge and rank known event versions.Non-NULL by query lineage, not a declared NOT NULL constraint. Inherited source value; this step ranks receipts without removing older versions.
version_rank · bigintPosition within this logical event ordered by latest receipt and then receipt ID.Non-NULL by query lineage, not a declared NOT NULL constraint. ROW_NUMBER is non-NULL; event_id is not unique until one version is retained.

This CTE is statement-local; it has no independently declared primary key, foreign key or NOT NULL constraint.

All source fields remain exposed. The CTE lasts only for this statement; cutoffs restrict knowledge before ranking.

Relationship map

Aliases name query roles, not extra tables: e = game_observed, p = game_players, a = game_activity, h = game_player_history.

Each game_players row can match zero or many game_observed rows; each game_observed row can match zero or one game_players rows. The stated keys and grouped or deduplicated grain determine these match limits; a child matching column can repeat unless its constraints or query grain make that value unique. Column names alone do not guarantee uniqueness. This is a logical query match, not a declared foreign key between these inputs. Missing or unmatched references need not have a parent.

Each game_activity row can match zero or one game_players rows; each game_players row can match zero or many game_activity rows. The stated keys and grouped or deduplicated grain determine these match limits; a child matching column can repeat unless its constraints or query grain make that value unique. Column names alone do not guarantee uniqueness. This is a logical query match, not a declared foreign key between these inputs. Missing or unmatched references need not have a parent.

Each game_events row can match zero or one game_players rows; each game_players row can match zero or many game_events rows. The stated keys and grouped or deduplicated grain determine these match limits; a child matching column can repeat unless its constraints or query grain make that value unique. Column names alone do not guarantee uniqueness. This is a logical query match, not a declared foreign key between these inputs. Missing or unmatched references need not have a parent.

An inner join omits nonmatches. A left join retains its left population and a full join retains both populations, using NULLs only for the missing side. These outer-join NULLs differ from unknown values stored in a real row. A one-to-many detail match repeats the other side's fields; preaggregate before summing parent-level money. An EXISTS test only asks whether a match exists and does not copy all matching child rows.

The snapshot uses UTC, an exclusive April 1, 2025 00:00 cutoff, baseline March 4 inclusive to March 18 exclusive, and follow-up March 18 inclusive to April 1 exclusive. Only accounts with full follow-up opportunity and valid baseline play enter the inactivity denominator. An absent play in an incomplete feed would be unknown observation, not demonstrated inactivity.

The team wants to know where the observed losses occur. Group eligible accounts by signup month and acquisition channel, preserving unknown channel as its own labeled bucket. The denominator is eligible accounts in that same cell, not all signups in the month and not event rows. These cohorts differ in account age; they are not an age-standardized comparison. A cell with one player can show 100% without supporting a broad conclusion. Always publish counts beside percentages.

Continue Lantern Trail with the same April 1, 2025 UTC exclusive cutoff, March 4–18 baseline, and March 18–April 1 follow-up. The first stage defines every field and validity rule. This lesson independently restores all earlier views; its starter includes the complete previous-stage script. Keep those definitions and add the new checkpoint after the previous final SELECT. The final SELECT is the result that Submit grades.

Your Task

  1. Create game_cohorts at signup-month/channel grain for inactive and retained accounts only. Use COALESCE(channel, 'unknown'). Return signup_month formatted as YYYY-MM-DD text (cast the date to text in the final SELECT), channel, eligible_players, inactive_players, retained_players, and inactivity_pct rounded to two decimals. Order by signup_month, then channel. The percentage is 100.0 times inactive divided by eligible, guarded with NULLIF.

Edit script.sql, Run to inspect the result, then Submit. Grading checks the complete ordered result, not whether a view was created or which equivalent SQL you used. Run and Submit reset the supplied snapshot.

Hint 1
1 / 1
HINT 1

Each cell needs its own eligible-player denominator, not all registrations.

Files
  • ⋄script.sql
Loading editor…
Press Run to execute your code.