Write a SQL query using window functions to calculate a rolling 7-day average for each product or user.
Use entities and daily_metrics. Include only products and users with metric observations. The rolling period includes the current date and the preceding six calendar days; missing dates do not contribute values.
entity_name, entity_type, metric_date, metric_value, rolling_7_day_avgentity_type, entity_id, and metric_date ascending| Column | Type | Description |
|---|---|---|
| entity_idPK | INT | Unique identifier for a product or user |
| entity_type | VARCHAR(20) | Entity classification, such as Product or User |
| entity_name | VARCHAR(100) | Display name of the entity |
| Column | Type | Description |
|---|---|---|
| metric_idPK | INT | Unique identifier for a daily metric observation |
| entity_id | INT | Referenced product or user identifier |
| metric_date | DATE | Date of the metric observation |
| metric_value | DECIMAL(10,2) | Metric value recorded for the entity and date |