Operations & Supply Chain Analytics · Mission 12 of 30Medium

Latest inventory on hand per warehouse

Latest-snapshot pattern (MAX(date)) + GROUP BY warehouse.

The Brief

Devon ParkWarehouse Leadslack-dm

For my morning huddle: total on-hand quantity per warehouse, using only the latest snapshot. Pull from `fact_inventory_snapshots` joined to `dim_warehouses`. Use `MAX(snapshot_date)` to get the latest, then SUM `on_hand_qty` per warehouse. Fair warning — that table has some duplicate rows (same SKU/warehouse/date appears twice for a few). Use DISTINCT or you'll double-count.

You'll practice

Latest-snapshot patternDISTINCTGROUP BY

Tables & columns available

fact_inventory_snapshotsfact6 columns
ColumnTypeKey
snapshot_idINTPK
sku_idINTFK → dim_skus
warehouse_idINTFK → dim_warehouses
snapshot_dateTEXT
on_hand_qtyINT
days_of_supplyREAL
dim_warehousesdim3 columns
ColumnTypeKey
warehouse_idINTPK
warehouse_nameTEXT
regionTEXT

Hints (4)

Each hint you reveal reduces the XP you can earn. Try the query first.

Hint 1

Two-step query: first find the latest snapshot date (`SELECT MAX(snapshot_date) FROM fact_inventory_snapshots`), then filter snapshots to that date.

Hint 2

Use a CTE or subquery: `WHERE snapshot_date = (SELECT MAX(snapshot_date) FROM fact_inventory_snapshots)`.

Hint 3

Wrap in DISTINCT on (sku_id, warehouse_id) before SUM, or use `SELECT DISTINCT sku_id, warehouse_id, on_hand_qty` in a subquery, then GROUP BY warehouse.

Hint 4

JOIN to `dim_warehouses` for the name. SUM(on_hand_qty), GROUP BY warehouse_name, ORDER BY total_on_hand DESC.