Write a query using window functions to find the top three most-ordered food items for each city on a monthly basis.
Use completed orders only and treat ordered quantity as the ranking measure. Return only the top three items per city and month.
Output
- One row per city, month, and qualifying food item.
- Columns:
city, month, food_item, total_quantity, and item_rank.
- Order by
city, month, item_rank, then food_item.
Output details
- Format
month as YYYY-MM.
- Rank items by
total_quantity descending.
- Break equal quantities by
food_item ascending.
- Assign unique ranks and retain exactly three items per city and month.
- Sort
city, month, item_rank, and food_item in ascending order.