Reconcile the latest approved stock count
Original fictional practice data · 2026-11-11 · CC BY 4.0

BRIEF
Warehouse staff upload multiple count attempts. Compare the latest approved count for each SKU with book stock, while retaining items that have never been approved.

CONTRACT
1. Use approved=1 records only; timestamps are ISO text with no ties per SKU.
2. Select the latest approved count independently for each SKU, not the latest timestamp for the warehouse.
3. Return sku,book_units,counted_units,variance_units. If no approved count exists, leave both derived numbers NULL.

METHOD
Filter out drafts before ranking count attempts.
Use ROW_NUMBER partitioned by SKU and descending timestamp.
LEFT JOIN the top count to book stock; calculate counted minus book only for a real count.

COMMON MISTAKE
Replacing missing counts with zero invents a stock loss for DRIVE. Zero is a valid approved count for LABEL.

INDEPENDENT CROSS-CHECK
Approved variances sum to zero across three counted SKUs; DRIVE still requires a count.

DELIVER
A checked output CSV and a handoff note explaining the exception policy. Use setup.sql in SQLite or import each table CSV into your spreadsheet. expected.csv is the complete reference result; an empty CSV field represents SQL NULL. The browser executes SQLite, not Excel formulas. XLOOKUP is unavailable in Excel 2016 and 2019.

Sources for function behavior: https://www.sqlite.org/lang_select.html and https://support.microsoft.com/en-us/excel/functions/xlookup-function (checked 2026-11-11). Rates, dates and policies here are exercise assumptions.
