Business Context
CVS Health operations leaders need a store-level view of sales, profitability, and inventory health across regions. The analysis should preserve stores with no sales or inventory snapshot so operational gaps remain visible.
Task
Write a PostgreSQL query for Q1 2025 that combines stores, sales, and inventory data without double-counting sales because of multiple inventory snapshots.
Requirements
- Aggregate net sales and cost by store, then calculate margin percentage.
- Select each store's most recent inventory snapshot in the period using a window function.
- Calculate regional average sales and margin, plus each store's sales rank within its region.
- Mark a store as
UNDERPERFORMING when sales are below 75% of its regional average, margin is below the regional average, or latest inventory units equal zero. Otherwise mark it PERFORMING.
- Retain stores with no sales and order results by region, sales rank, and store ID.
Representative Data