Subscription service and billing · Original fictional data · Free files and full answers
Calculate unused service credited at cancellation
Compare prepaid paid service through min(end_day,30) with service ending at cancel_day. Return cancelled contract_id,unused_days,credit_cents; only contracts with cancellation before month-end qualify. Sort contract_id.
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
- Compare prepaid paid service through min(end_day,30) with service ending at cancel_day. Return cancelled contract_id,unused_days,credit_cents; only contracts with cancellation before month-end qualify. Sort contract_id.
- 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.
- 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 contract_id, unused_days, credit_cents. Sort order follows the task. Output has 2 rows; an empty CSV field means NULL.
Source data
contracts
6 source rows · Download contracts CSV
| id INTEGER | start_day INTEGER | end_day INTEGER | seats INTEGER | daily_cents INTEGER | trial_end INTEGER | cancel_day INTEGER |
|---|---|---|---|---|---|---|
| 1 | 0 | 30 | 2 | 100 | 3 | NULL |
| 2 | 5 | 20 | 1 | 150 | 5 | 15 |
| 3 | 10 | 40 | 3 | 80 | 12 | NULL |
| 4 | 0 | 10 | 1 | 200 | 0 | 8 |
| 5 | 20 | 30 | 2 | 120 | 25 | NULL |
| 6 | 0 | 60 | 1 | 50 | 0 | NULL |
usage
9 source rows · Download usage CSV
| id INTEGER | contract_id INTEGER | day INTEGER | units INTEGER |
|---|---|---|---|
| 1 | 1 | 3 | 40 |
| 2 | 1 | 4 | 80 |
| 3 | 1 | 8 | 20 |
| 4 | 2 | 6 | 50 |
| 5 | 2 | 16 | 10 |
| 6 | 3 | 15 | 250 |
| 7 | 4 | 3 | 0 |
| 8 | 5 | 27 | 80 |
| 9 | 6 | 10 | 120 |
bills
5 source rows · Download bills CSV
| id INTEGER | contract_id INTEGER | start_day INTEGER | end_day INTEGER | cents INTEGER |
|---|---|---|---|---|
| 1 | 1 | 3 | 10 | 1400 |
| 2 | 1 | 12 | 30 | 3600 |
| 3 | 2 | 5 | 15 | 1500 |
| 4 | 4 | 0 | 8 | 1600 |
| 5 | 6 | 0 | 30 | 1500 |
Field dictionary and schema
contracts: id INTEGER — Stable source record identifier; not a row position. start_day INTEGER — Day index or ISO date as declared in this task; preserve its source representation. end_day INTEGER — Day index or ISO date as declared in this task; preserve its source representation. seats INTEGER — Number of paid service seats. daily_cents INTEGER — Contracted cents per seat per day. trial_end INTEGER — First day of paid service, after the free trial. cancel_day INTEGER — Exclusive service end day if cancelled; NULL means no recorded cancellation. usage: id INTEGER — Stable source record identifier; not a row position. contract_id INTEGER — Foreign business-record identifier; match to the named source key. day INTEGER — Integer day index, or ISO YYYY-MM-DD date when stored as text; the task declares cutoff semantics. units INTEGER — Physical unit quantity, not a monetary amount. bills: id INTEGER — Stable source record identifier; not a row position. contract_id INTEGER — Foreign business-record identifier; match to the named source key. start_day INTEGER — Day index or ISO date as declared in this task; preserve its source representation. end_day INTEGER — Day index or ISO date as declared in this task; preserve its source representation. cents INTEGER — Signed integer currency cents.
A method you can reuse
- Compare prepaid paid service through min(end_day,30) with service ending at cancel_day. Return cancelled contract_id,unused_days,credit_cents; only contracts with cancellation before month-end qualify. Sort contract_id.
- For the first output record, contract_id = 2; unused_days = 5; credit_cents = 750.
- Inspect the source grain, keys and ties. Compare each result column with the full worked SQL and expected CSV.
Watch for this wrong turn
Crediting from cancellation to the original end beyond the prepaid month refunds unbilled future service.
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
SELECT id contract_id,MAX(0,MIN(end_day,30)-MAX(start_day,trial_end,cancel_day)) unused_days,MAX(0,MIN(end_day,30)-MAX(start_day,trial_end,cancel_day))*seats*daily_cents credit_cents FROM contracts WHERE cancel_day IS NOT NULL AND cancel_day<MIN(end_day,30) ORDER BY id;
"contract_id","unused_days","credit_cents" "2","5","750" "4","2","400"Reference SQL · Complete expected CSV · Empty answer CSV
Choose another SQL task · Original data exception cases · Larger workplace projects