🏠 Trang chủ
Benchmark
📊 Tất cả benchmark 🦖 Khủng long v1 🦖 Khủng long v2 ✅ Ứng dụng To-Do List 🎨 Trang tự do sáng tạo 🎯 FSACB - Trình diễn cuối cùng 🌍 Benchmark dịch thuật
Mô hình
🏆 Top 10 mô hình 🆓 Mô hình miễn phí 📋 Tất cả mô hình ⚙️ Kilo Code
Tài nguyên
💬 Thư viện prompt 📖 Thuật ngữ AI 🔗 Liên kết hữu ích
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.