Write a SQL query to identify Acme Fresh Market stores with the highest inventory shrinkage by month, joining inventory counts, sales, and store metadata tables. Calculate shrinkage as opening inventory plus received units minus sold units minus counted units. Include tied stores at the highest shrinkage for each month.
month_start, store_id, store_name, shrinkage_units, shrinkage_rate, and month_rankmonth_start, then month_rank, then store_id| Column | Type | Description |
|---|---|---|
| store_idPK | INT | Unique Acme Fresh Market store identifier |
| store_name | VARCHAR(100) | Store display name |
| city | VARCHAR(100) | City where the store operates |
| Column | Type | Description |
|---|---|---|
| count_idPK | INT | Unique inventory count identifier |
| store_id | INT | Store associated with the count |
| count_date | DATE | Date of the physical inventory count |
| opening_units | INT | Units available at the start of the period |
| received_units | INT | Units received during the period |
| counted_units | INT | Units physically counted at period end |
| Column | Type | Description |
|---|---|---|
| sale_idPK | INT | Unique sales transaction identifier |
| store_id | INT | Store where the sale occurred |
| sale_date | DATE | Date of the sale |
| units_sold | INT | Units sold in the transaction |