Your question is Pivot Diagnosis Counts by Category. Start with the requirements and the four 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.
Novo Nordisk needs a monthly view of diagnosed patients by clinical category for analytics reporting. Write a PostgreSQL query that pivots selected diagnosis categories into separate columns.
| Column | Type | Description |
|---|---|---|
| patient_idPK | INT | Patient identifier |
| patient_name | VARCHAR(100) | Patient name |
| country | VARCHAR(80) | Patient country |
| analytics_consent | BOOLEAN | Whether analytics use is permitted |
| Column | Type | Description |
|---|---|---|
| encounter_idPK | INT | Encounter identifier |
| patient_id | INT | Referenced patient |
| encounter_date | DATE | Date of encounter |
| status | VARCHAR(30) | Encounter status |
| Column | Type | Description |
|---|---|---|
| diagnosis_event_idPK | INT | Diagnosis event identifier |
| encounter_id | INT | Referenced encounter |
| patient_id | INT | Referenced patient |
| category_id | INT | Referenced diagnosis category |
| diagnosis_code | VARCHAR(20) | Clinical diagnosis code |
| Column | Type | Description |
|---|---|---|
| category_idPK | INT | Category identifier |
| category_name | VARCHAR(100) | Clinical diagnosis category |
| therapy_area | VARCHAR(100) | Broader therapy area |