Write a SQL query to analyze monthly property operating expenses by building, compare them to budget, and flag properties with more than 10% variance.
Use the properties, operating_expenses, and expense_budgets tables. Include months represented by either actual expenses or budgets, and treat a budget of zero or less as having no positive budget for percentage comparison.
Output
- One row per month and building, ordered by month ascending and building ID ascending.
- Columns:
month, property_id, building_id, building_name, actual_expenses, budget_expenses, variance_amount, variance_percent, and variance_flag.
- Include unmatched actual or budget records.
variance_percent is NULL when the budget is not positive; use OVER_10_PERCENT only when the absolute variance exceeds 10%.