A new stock positions mart (a table built for reporting) is about to replace a spreadsheet the warehouse team trusts. The reviewer will approve it only with evidence that it keeps its contract, meaning the promises it makes to the people who use it: one row per product and warehouse, no missing keys, and unit totals that tie back to the source tables.
check_name, expected, actual, and status (pass when expected equals actual, otherwise fail), one row per check, ordered by check_name. Build the mart in a CTE first, with one row for each row of inventory_opening_balances: product_id, warehouse_id, and closing_units, which is opening_units plus the sum of quantity_change in inventory_movements for that product and warehouse, or plus 0 when it has no movements.The four checks are grain_is_unique (expected: the number of distinct product_id and warehouse_id pairs in the mart; actual: the number of rows in the mart), keys_not_null (expected: 0; actual: the number of mart rows where product_id or warehouse_id is NULL), rows_match_source (expected: the number of rows in inventory_opening_balances; actual: the number of rows in the mart), and units_reconcile (expected: the sum of opening_units plus the sum of quantity_change over every row of inventory_movements; actual: the sum of closing_units in the mart).
Create a free account to save your progress and reveal the worked answer for this free exercise. The complete practice library is available with Practice Pass or Mastery.