← SQL workplace practice

Basket analysis and allocation · Original fictional data · Free files and full answers

Allocate one hundred cents without losing rounding residuals

Allocate a fixed 100-cent order credit across positive-gross lines in proportion to line gross. Floor each share, then assign residual cents to largest fractional remainders, ties by sku. Return order_id,sku,credit_cents; exclude zero-gross lines. Sort order_id,sku.

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. Allocate a fixed 100-cent order credit across positive-gross lines in proportion to line gross. Floor each share, then assign residual cents to largest fractional remainders, ties by sku. Return order_id,sku,credit_cents; exclude zero-gross lines. Sort order_id,sku.
  2. Return exactly the declared columns and order. Money uses integer cents; all time values are elapsed UTC minutes unless a field is explicitly a day index. NULL is unknown and differs from numeric zero.
  3. These are original fictional records and exercise policies. All source tables in this case are included in the downloadable pack.

Return columns exactly as order_id, sku, credit_cents. Sort order follows the task. Output has 17 rows; an empty CSV field means NULL.

Source data

basket

18 source rows · Download basket CSV

order_id INTEGERsku TEXTqty INTEGERprice INTEGERcategory TEXT
1A2500book
1B11200tool
1C1500book
2A1500book
2D3300food
3B21200tool
3E1800food
4C3500book
4D1300food
5A1500book
5B11200tool
5E2800food
6F10gift
6D2300food
7C1500book
7E1800food
8A1500book
8C2500book

required

2 source rows · Download required CSV

sku TEXT
A
B
Field dictionary and schema
basket:
  order_id INTEGER — Stable record or business key; retain its declared type.
  sku TEXT — Literal text label or key; comparisons use SQLite binary text semantics.
  qty INTEGER — Exact integer measurement; use the task rule for units and ordering.
  price INTEGER — Exact integer measurement; use the task rule for units and ordering.
  category TEXT — Literal text label or key; comparisons use SQLite binary text semantics.

required:
  sku TEXT — Literal text label or key; comparisons use SQLite binary text semantics.

Download complete SQLite setup

A method you can reuse

  1. Allocate a fixed 100-cent order credit across positive-gross lines in proportion to line gross. Floor each share, then assign residual cents to largest fractional remainders, ties by sku. Return order_id,sku,credit_cents; exclude zero-gross lines. Sort order_id,sku.
  2. Inspect the source grain, keys and ties before writing the query. Use the worked SQL and complete expected CSV to review any mismatch.

Watch for this wrong turn

Rounding every line independently can distribute 99 or 101 cents instead of exactly 100.

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 x AS (SELECT *,qty*price g,SUM(qty*price) OVER(PARTITION BY order_id) total FROM basket WHERE qty*price>0), y AS (SELECT *,100*g/total base,ROW_NUMBER() OVER(PARTITION BY order_id ORDER BY (100*g)%total DESC,sku) rn FROM x), z AS (SELECT *,100-SUM(base) OVER(PARTITION BY order_id) residual FROM y) SELECT order_id,sku,base+(rn<=residual) credit_cents FROM z ORDER BY order_id,sku;
"order_id","sku","credit_cents"
"1","A","37"
"1","B","44"
"1","C","19"
"2","A","36"
"2","D","64"
"3","B","75"
"3","E","25"
"4","C","83"
"4","D","17"
"5","A","15"
"5","B","36"
"5","E","49"
"6","D","100"
"7","C","38"
"7","E","62"
"8","A","33"
"8","C","67"
Reference SQL · Complete expected CSV · Empty answer CSV

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