🏠 首页
基准测试
📊 所有基准测试 🦖 恐龙 v1 🦖 恐龙 v2 ✅ 待办事项应用 🎨 创意自由页面 🎯 FSACB - 终极展示 🌍 翻译基准测试
模型
🏆 前 10 名模型 🆓 免费模型 📋 所有模型 ⚙️ 🛠️ 千行代码模式
资源
💬 💬 提示库 📖 📖 AI 词汇表 🔗 🔗 有用链接
advanced

Complex SQL Data Analysis

#sql #data-analysis #window-functions #optimization

Solve a multi-step data analysis problem using advanced SQL window functions, CTEs, and performance optimization.

You are a Data Analyst. You have access to a database with three tables: 'Sales' (transaction_id, date, amount), 'Products' (product_id, category, cost), and 'Stores' (store_id, region, manager). Your task is to write a single PostgreSQL query that performs the following: 1. Calculates the running total of sales amount per store ordered by date. 2. Identifies the difference in daily sales compared to the previous day for each store (lag function). 3. Ranks the stores within each region based on their total monthly sales (dense_rank function). 4. Filters the results to show only the top-performing store for each region for the current month. 5. Explain how you would index these tables to optimize the performance of this specific query.