Advanced
SQL Window Functions Optimization
Write a complex SQL query using window functions to calculate moving averages and ranking without self-joins.
📝 Contenu du Prompt
Given a sales table with columns (transaction_id, sale_date, amount, region), write a single SQL query that calculates the 3-day moving average of sales per region and ranks each transaction within its region by amount in descending order. Use only window functions (OVER, PARTITION BY, ROWS/RANGE) and avoid using subqueries or self-joins. Optimize for execution on a PostgreSQL database.