Ivy Professional School
Rating
Learning/SQL Data Drill #8

Inventory vs. Sales Demand Alignment

Last updated

Evaluate whether each store is maintaining inventory levels in line with monthly product demand.

SQLIntermediateCTEWindow FunctionsSUM OVERIF()

Your Objective

Evaluate whether each store is maintaining inventory levels in line with monthly product demand.

The Setup

Your dataset contains three tables from a retail inventory system: 1. An Inventory_Movements table tracking stock added or removed per product per store over time 2. An Orders table with each order placed at a store 3. An Order_Items table with the product and quantity sold in each order

Table Schema

Inventory_Movements
ColumnTypeKey / Notes
movement_idINTPK
store_idINT
product_idINT
movement_dateDATE
quantity_deltaINT
Orders
ColumnTypeKey / Notes
order_idINTPK
store_idINT
order_dateDATE
Order_Items
ColumnTypeKey / Notes
order_item_idINTPK
order_idINTFK
product_idINT
quantityINT

Sample Data

Inventory_Movements — 12 rows

movement_idstore_idproduct_idmovement_datequantity_delta
111012024-01-0150
211012024-02-0120
311012024-03-0115
411022024-01-0140
511022024-02-0110
611022024-03-015
721012024-01-0160
821012024-02-0125
921012024-03-0120
1021022024-01-0135
1121022024-02-0115
1221022024-03-0110

Orders — 12 rows

order_idstore_idorder_date
112024-01-10
212024-02-12
312024-03-15
412024-01-20
512024-02-22
612024-03-25
722024-01-11
822024-02-13
922024-03-17
1022024-01-21
1122024-02-23
1222024-03-27

Order_Items — 12 rows

order_item_idorder_idproduct_idquantity
1110148
2210170
3310195
4410225
5510260
6610255
7710158
8810180
99101120
101010234
111110250
121210265

Your Task

Write a SQL query that evaluates whether each store maintains inventory levels in line with monthly sales demand. Your query should: 1. Calculate the monthly on-hand (cumulative) inventory for each product at each store 2. Calculate the total monthly sales for each product at each store 3. Compare inventory with sales and classify as: • "Within Demand" — if inventory is within ±5 units of sales • "Inventory Issue" — otherwise Your result table should have: store_id, product_id, mnth_yr, classification.

Your output should look like this (partial example):

store_idproduct_idmnth_yrclassification
1101January, 2024Within Demand
1101February, 2024Within Demand
…………

Hints

  • →Start with a CTE that groups inventory_movements by store_id, product_id, year, and month — SUM(quantity_delta) gives the monthly stock change.
  • →Wrap that CTE in a second step and use SUM(on_hand_inventory) OVER (PARTITION BY store_id, product_id ORDER BY yr, mnth) to get the running cumulative inventory.
  • →Write a separate CTE for sales: INNER JOIN orders with order_items, then group by store_id, product_id, year, and month — SUM(quantity) gives monthly sales.
  • →Join the inventory and sales CTEs on store_id, product_id, yr, and mnth_name. Use IF(cumulative BETWEEN sales-5 AND sales+5, "Within Demand", "Inventory Issue") for classification.
Explore Enterprise Solutions