Write a SQL query to identify the top-performing Walgreens Boots Alliance stores by monthly sales and compare each store's sales to its district average. Return the top-ranked store or tied stores for every district and calendar month. Include stores with no sales in a month when calculating district averages.
Output
- One row per top-ranked store, district, and month
- Columns:
month_start, district_name, store_id, store_name, monthly_sales, district_average, and sales_vs_district_avg_pct
- Include ties, calculate the percentage difference from the district average, and order by month, district, and store ID
Output details
- Use only distinct calendar months represented by
sale_date.
- Treat stores with no monthly sales as
0.
- Round
district_average to 2 decimal places before calculating the percentage difference.
- Round
sales_vs_district_avg_pct to 2 decimal places.
- Return
0.00 for sales_vs_district_avg_pct when district_average is zero.
- Sort
month_start, district_name, and store_id ascending.