Practical Window Function Patterns in PostgreSQL
📖 Scenario: You are working as a data analyst for a retail company. You have a table called sales that records each sale transaction with columns for sale_id, store_id, sale_date, and amount. Your manager wants to analyze sales trends by store and over time.
🎯 Goal: Build a PostgreSQL query using window functions to calculate the running total of sales amount per store ordered by sale date, and also rank each sale within its store by amount.
📋 What You'll Learn
Use the
sales table with columns sale_id, store_id, sale_date, and amountCalculate a running total of
amount per store_id ordered by sale_dateRank each sale within its
store_id by amount in descending orderUse window functions
SUM() OVER() and RANK() OVER()💡 Why This Matters
🌍 Real World
Retail analysts often need to track cumulative sales and rank transactions to identify top-performing stores or sales days.
💼 Career
Understanding window functions is essential for data analysts and database professionals to perform advanced data analysis and reporting.
Progress0 / 4 steps