Your question is Rolling 7-Day Sales Trends. Start with the requirements and the three tables on the right.
Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.
How would you use SQL window functions to calculate rolling 7-day sales trends by product and region for W.W. Grainger?
Write a PostgreSQL query using the provided sales, products, and regions data. Include dates with sales activity, calculate daily sales, and calculate the total for the current date plus the preceding six calendar days.
product_name, region_name, sales_date, daily_sales, and rolling_7_day_sales.product_name, region_name, and sales_date ascending.| Column | Type | Description |
|---|---|---|
| sale_idPK | INT | Unique sales transaction identifier |
| product_id | INT | Product associated with the sale |
| region_id | INT | Sales region associated with the transaction |
| sales_date | DATE | Date of the sale |
| sales_amount | NUMERIC(12,2) | Sales amount for the transaction |
| Column | Type | Description |
|---|---|---|
| product_idPK | INT | Unique product identifier |
| product_name | VARCHAR(100) | Grainger product name |
| Column | Type | Description |
|---|---|---|
| region_idPK | INT | Unique region identifier |
| region_name | VARCHAR(100) | Sales region name |