Write a SQL query using window functions to compute a rolling 7-day average of test positivity by patient cohort.
Use the patients and test_results tables. Exclude tests with missing results or dates. Calculate positivity per cohort and test date, then return the rolling average over the current date and preceding six calendar days.
cohort, test_date, test_count, positive_tests, positivity_rate, and rolling_7_day_avgcohort, then test_datecohort and test_date in ascending order.positivity_rate and rolling_7_day_avg to four decimal places.test_result equals positive.| Column | Type | Description |
|---|---|---|
| patient_idPK | INT | Unique patient identifier |
| cohort | VARCHAR(50) | Patient cohort label |
| Column | Type | Description |
|---|---|---|
| test_idPK | INT | Unique test identifier |
| patient_id | INT | Patient who received the test |
| test_date | DATE | Date the test was performed |
| test_result | VARCHAR(20) | Test outcome, such as positive or negative |