← SQL workplace practice

Experiment data quality · Original fictional data · Free files and full answers

Calculate treatment differences within enrollment strata

For each first-assignment user sum eligible outcome values, zero if absent. Per stratum return stratum,mean_A,mean_B,difference_B_minus_A rounded2. Both arms are present in each supplied stratum. Sort stratum.

Use this fixed business case to build a checked CSV and a replayable SQL handoff. No account required. Scenario datasets are shared across related exercises; each exercise has its own decision and output contract.

Your output contract

  1. For each first-assignment user sum eligible outcome values, zero if absent. Per stratum return stratum,mean_A,mean_B,difference_B_minus_A rounded2. Both arms are present in each supplied stratum. Sort stratum.
  2. Return exactly the declared columns and order. Money uses integer cents; time and date units follow the task and field dictionary. NULL is unknown and differs from numeric zero.
  3. These are original fictional records and exercise policies. Scenario data may be shared with related tasks, whose decisions and output contracts differ.

Return columns exactly as stratum, mean_A, mean_B, difference_B_minus_A. Sort order follows the task. Output has 2 rows; an empty CSV field means NULL.

Source data

assignments

12 source rows · Download assignments CSV

id INTEGERuser_id INTEGERvariant TEXTminute INTEGERdevice TEXTstratum TEXT
11A0phonenew
21A0phonenew
32B0phonenew
43A2webold
53B5webold
64B2webold
75A3phonenew
86B4phoneold
97A5webnew
108B6webnew
119A7phoneold
1210B8webold

outcomes

11 source rows · Download outcomes CSV

id INTEGERuser_id INTEGERminute INTEGERvalue INTEGERexposed INTEGERerror INTEGER
1141010
2162010
32-15010
425011
53810010
64103010
758000
861250011
9712010
108154010
119201010
Field dictionary and schema
assignments:
  id INTEGER — Stable source record identifier; not a row position.
  user_id INTEGER — Foreign business-record identifier; match to the named source key.
  variant TEXT — Literal business key or category label. Match exactly and retain NULL where present.
  minute INTEGER — Elapsed UTC minute from the fictional scenario origin.
  device TEXT — Literal business key or category label. Match exactly and retain NULL where present.
  stratum TEXT — Literal business key or category label. Match exactly and retain NULL where present.

outcomes:
  id INTEGER — Stable source record identifier; not a row position.
  user_id INTEGER — Foreign business-record identifier; match to the named source key.
  minute INTEGER — Elapsed UTC minute from the fictional scenario origin.
  value INTEGER — Measured numeric value; NULL means unobserved.
  exposed INTEGER — Exact integer business measurement. Its specific business rule and eligibility are declared in the task.
  error INTEGER — Exact integer business measurement. Its specific business rule and eligibility are declared in the task.

Download complete SQLite setup

A method you can reuse

  1. For each first-assignment user sum eligible outcome values, zero if absent. Per stratum return stratum,mean_A,mean_B,difference_B_minus_A rounded2. Both arms are present in each supplied stratum. Sort stratum.
  2. For the first output record, stratum = new; mean_A = 10; mean_B = 20; difference_B_minus_A = 10.
  3. Inspect the source grain, keys and ties. Compare each result column with the full worked SQL and expected CSV.

Watch for this wrong turn

Pooling strata before comparing arms can confound the treatment difference with enrollment composition.

Run and check in SQLite

Your SQL runs locally in the existing SQLite worker. The checker compares every column, row, type, value and row order with this fixed output. It accepts equivalent SELECT or WITH queries. Browser storage keeps your SQL on this device when available.

Complete worked SQL and expected output
WITH r AS (SELECT *,ROW_NUMBER() OVER(PARTITION BY user_id ORDER BY minute,id) n FROM assignments), u AS (SELECT a.user_id,a.stratum,a.variant,COALESCE(SUM(o.value),0) revenue FROM r a LEFT JOIN outcomes o ON o.user_id=a.user_id AND o.minute>=a.minute WHERE n=1 GROUP BY a.user_id) SELECT stratum,ROUND(AVG(CASE WHEN variant='A' THEN revenue END),2) mean_A,ROUND(AVG(CASE WHEN variant='B' THEN revenue END),2) mean_B,ROUND(AVG(CASE WHEN variant='B' THEN revenue END)-AVG(CASE WHEN variant='A' THEN revenue END),2) difference_B_minus_A FROM u GROUP BY stratum ORDER BY stratum;
"stratum","mean_A","mean_B","difference_B_minus_A"
"new","10","20","10"
"old","55","176.67","121.67"
Reference SQL · Complete expected CSV · Empty answer CSV

Choose another SQL task · Original data exception cases · Larger workplace projects